Random scope between enemy and ally

Hello,i've wondered if it's possible to create a random chance(let's say,with a percentage of 50%) of an item to affect either an enemy or an ally.
And to be more specific,i'm trying to create an item that will deal 100 damage for an enemy,but if the ally is chosen,it will deal 50 for the ally instead.

This is probably too complex,but i'm just wondering if there are any possible ways to make it.😅
Hmm... I would do with just scope "the user" and include main target within the JS function.
JavaScript:
(function(){

    'use strict';
  
    Game_Player.prototype.mySkill = function(a, b, index){
  
        var party = $gameParty;
        var troop = $gameTroop;
        var seed = Math.randomInt(100);
      
        /** Set your formula here **/
        var formula = 0;
      
        if (seed <= 50){
            party.members()[index].gainHp(-formula / 2);
            party.members()[index].startAnimation(animationId);
            party.members()[index].startDamagePopup();
            party.members()[index].requestMotion('damage');
        } else {
            troop.members()[index].gainHp(-formula);
            troop.members()[index].startAnimation(animationId);
            troop.members()[index].startDamagePopup();
            troop.members()[index].requestMotion('damage');          
        }
        return 0;
    }
})();

0 - means leader.

Inside the function use vars a and b to define skill formula

This code generates a random number from 1 to 100, if the number is below 50, hit the ally with the given index, otherwise hit enemy in battle.

just fill your skill formula like that, and save my code as plugin and add to your game.

include main target within the JS function.
This got me kind of confused...how do I include a main target?
Also,did i wrote this right?it doesn't seem to work for now😅
JavaScript:
(function(){

    'use strict';
  
    Game_Player.prototype.mySkill = function(a, b, index){
  
        var party = $gameParty;
        var troop = $gameTroop;
        var seed = Math.randomInt(100);
      
          a.atk*3
        var formula = 0;
      
        if (seed <= 50){
            party.members()[index].gainHp(-formula);
            party.members()[index].startAnimation(animationId);
            party.members()[index].startDamagePopup();
            party.members()[index].requestMotion('damage');
        } else {
            troop.members()[index].gainHp(-formula);
            troop.members()[index].startAnimation(animationId);
            troop.members()[index].startDamagePopup();
            troop.members()[index].requestMotion('damage');           
        }
        return 0;
    }
})();
Ehh, when you set your skill's scope The User that skill won't start selecting opponent, so I can use Math.randomInt() method to determine target of spell, without actually selecting it.

The Scope The User means, the spell will be casted upon a battler that is using it, so inside function I'm selecting target.

look at this fragment of code:
Code:
        var party = $gameParty;
        var troop = $gameTroop;
I declared handlers to define whether the skill (item) will target enemy or ally.
Now:

Code:
var seed = Math.randomInt(100);

seed receive random value upon using item/skill ranged between 1 and 100, so now with this conditional branch I can determine who the target is (ally if seed gets value below or equal 50 or enemy, if seed gets value higher than 50)
Code:
        if (seed <= 50){
            party.members()[index].gainHp(-formula);
            party.members()[index].startAnimation(animationId);
            party.members()[index].startDamagePopup();
            party.members()[index].requestMotion('damage');
        } else {
            troop.members()[index].gainHp(-formula);
            troop.members()[index].startAnimation(animationId);
            troop.members()[index].startDamagePopup();
            troop.members()[index].requestMotion('damage');           
        }

And No, your damage formula is declared wrong, it should be:
Code:
var formula = a.atk * 3;


JavaScript:
(function(){

    'use strict';
  
    Game_Player.prototype.mySkill = function(a, b, index){
  
        var party = $gameParty;
        var troop = $gameTroop;
        var seed = Math.randomInt(100);
      
          
        var formula = a.atk*3;
      
        if (seed <= 50){
            party.members()[index].gainHp(-formula);
            party.members()[index].startAnimation(animationId);
            party.members()[index].startDamagePopup();
            party.members()[index].requestMotion('damage');
        } else {
            troop.members()[index].gainHp(-formula);
            troop.members()[index].startAnimation(animationId);
            troop.members()[index].startDamagePopup();
            troop.members()[index].requestMotion('damage');           
        }
        return 0;
    }
})();

with variable named index you can determine who will be target from enemies or allies.
0 means leader (first battler in group)
Ehh, when you set your skill's scope The User that skill won't start selecting opponent, so I can use Math.randomInt() method to determine target of spell, without actually selecting it.

The Scope The User means, the spell will be casted upon a battler that is using it, so inside function I'm selecting target.

look at this fragment of code:
Code:
        var party = $gameParty;
        var troop = $gameTroop;
I declared handlers to define whether the skill (item) will target enemy or ally.
Now:

Code:
var seed = Math.randomInt(100);

seed receive random value upon using item/skill ranged between 1 and 100, so now with this conditional branch I can determine who the target is (ally if seed gets value below or equal 50 or enemy, if seed gets value higher than 50)
Code:
        if (seed <= 50){
            party.members()[index].gainHp(-formula);
            party.members()[index].startAnimation(animationId);
            party.members()[index].startDamagePopup();
            party.members()[index].requestMotion('damage');
        } else {
            troop.members()[index].gainHp(-formula);
            troop.members()[index].startAnimation(animationId);
            troop.members()[index].startDamagePopup();
            troop.members()[index].requestMotion('damage');           
        }

And No, your damage formula is declared wrong, it should be:
Code:
var formula = a.atk * 3;


JavaScript:
(function(){

    'use strict';
  
    Game_Player.prototype.mySkill = function(a, b, index){
  
        var party = $gameParty;
        var troop = $gameTroop;
        var seed = Math.randomInt(100);
      
          
        var formula = a.atk*3;
      
        if (seed <= 50){
            party.members()[index].gainHp(-formula);
            party.members()[index].startAnimation(animationId);
            party.members()[index].startDamagePopup();
            party.members()[index].requestMotion('damage');
        } else {
            troop.members()[index].gainHp(-formula);
            troop.members()[index].startAnimation(animationId);
            troop.members()[index].startDamagePopup();
            troop.members()[index].requestMotion('damage');           
        }
        return 0;
    }
})();

with variable named index you can determine who will be target from enemies or allies.
0 means leader (first battler in group)
Thanks a lot!

Original thread: https://forums.rpgmakerweb.com/threads/random-scope-between-enemy-and-ally.144012/