Passive that heal MP on Basic Attack

So I want to make passive that "have a 33% chance to Heal MP for entire Party" when this character basic attack.

Thanks
Does this work (using the DLC Kadokawa plugin LastUsedItem to get the skill ID to variables X and Y)?

State notetag:

Spoiler
<Custom Initiate Effect>
if ($gameVariables.value(X) == 1 && $gameVariables.value(Y) == 1 && Math.randomInt(2) == 0)
var actor = $gameParty.members()[0];
actor.gainMp(10);
actor.startDamagePopup();

var actor = $gameParty.members()[1];
actor.gainMp(10);
actor.startDamagePopup();

var actor = $gameParty.members()[2];
actor.gainMp(10);
actor.startDamagePopup();

var actor = $gameParty.members()[3];
actor.gainMp(10);
actor.startDamagePopup();
</Custom Initiate Effect>

You could probably simplify that and not write the same thing for every actor, but that's just an idea, LOL.
It work!!
I didn't know about the Plugin in DLC folder before LOL
Thanks!!
Does this work (using the DLC Kadokawa plugin LastUsedItem to get the skill ID to variables X and Y)?

State notetag:

Spoiler
<Custom Initiate Effect>
if ($gameVariables.value(X) == 1 && $gameVariables.value(Y) == 1 && Math.randomInt(2) == 0)
var actor = $gameParty.members()[0];
actor.gainMp(10);
actor.startDamagePopup();

var actor = $gameParty.members()[1];
actor.gainMp(10);
actor.startDamagePopup();

var actor = $gameParty.members()[2];
actor.gainMp(10);
actor.startDamagePopup();

var actor = $gameParty.members()[3];
actor.gainMp(10);
actor.startDamagePopup();
</Custom Initiate Effect>

You could probably simplify that and not write the same thing for every actor, but that's just an idea, LOL.
Alternatively, based on rpgLord69's answer (untested):

<Custom Initiate Effect> if (this.isAttack() && 3 * Math.random() < 1) { for (var actor of $gameParty.members()) { actor.gainMp(10); actor.startDamagePopup(); } } </Custom Initiate Effect>
This only requires YEP Buffs & States Core and handles battle party sizes other than 4. It also accounts for battlers possibly having different/multiple "attack" skills, e.g. via plugins. 👋

&& Math.randomInt(2) == 0
Careful with this! Math.randomInt(2) returns 0 or 1. For a 33% chance, you'd want Math.randomInt(3) here.

You also forgot the {braces} around your "if" content. As written, only the first var actor line is actually dependent on the conditions. 😅
I ended up with something like this:

Spoiler: Notetags
Code:
<Custom Battle Effect>
$gameParty.members().forEach((actor) => actor.clearResult());
</Custom Battle Effect>

Code:
<Custom Confirm Effect>
if (this._item._itemId == 1 && Math.randomInt(2) == 0 && this._subjectEnemyIndex < 0) {
for (var i = 0; i < $gameParty.aliveMembers().length; i++) {
 var actor = $gameParty.aliveMembers()[i];
 actor.gainMp(Math.max(Math.floor(value / 10), 1));
 if (i > 0) actor.startDamagePopup();
 };
};
</Custom Confirm Effect>

The problem I ran into with using a loop is that the damage popup doesn't automatically work for each member after the first.

Then, the problem with adding the damage pop up in the loop is that if you use the attack skill on your first actor's turn, it would display any pending damage pop-up result. An change hp/mp event command used in conjunction with the battle start event command would cause the hp/mp change from the event command to show in the initial damage popup if the attack skill was used before the system could clear each standing damage pop-up results.

The damage popup was also showing twice for the actor using the skill, which is why I added the second if statement.

So the first notetag would clear any previous damage popup result at the start of the battle.

Also note that you can use the existing 'value' variable to reference the amount of damage being done to calculate a relative amount of MP to recover, as opposed to a static amount.

For complete reference, I needed the '&& this._subjectEnemyIndex < 0' to make sure it wasn't an enemy using the attack skill because I used a global passive that is applied to all actors and battlers to test this. You may not need that, but you may need a way to check if the actor is targeting a party mamber, unless that is a function you actually want.

Oh and I also changed the timing of the notetag to Confirm Effect so the effect only applies when the attack lands.
The damage popup was also showing twice for the actor using the skill, which is why I added the second if statement.
The problem with this is that $gameParty.aliveMembers()[0] is one specific actor. It is not necessarily going to be the actor using the skill (in fact, 75% of the time it won't be).

If you want to skip it for the current actor, you should change it to:
Code:
if (actor!=user)

For complete reference, I needed the '&& this._subjectEnemyIndex < 0' to make sure it wasn't an enemy using the attack skill
This works, but could more simply be user.isActor()
@caethyril Good points! 😀

But I thought that it reads stuff inside the notetags differently. I tested it and it's only doing all the lines if the if statements are true. But maybe I'm not understanding something, and anyway that's not the point of this thread LOL.
But I thought that it reads stuff inside the notetags differently.
Nope. Anything a plugin says is JavaScript is JavaScript and follows standard syntax.
I tested it and it's only doing all the lines if the if statements are true.
It just gets passed through an eval. I think I see why you're seeing that, though: if var actor doesn't get defined, that causes an error when the next line tries to reference it. Yanfly's plugin processes the eval in a try-catch block, and just outputs a message to the console on error:
JavaScript:
    try {
      eval(code);
    } catch (e) {
      Yanfly.Util.displayError(e, code,
        'CUSTOM STATE ' + stateId + ' CODE ERROR');
    }
You could check the Console tab of the dev tools (F8 or F12 during playtest) to see if that error is appearing.

If that's the case, then an error will produce the same result as unmet conditions (plus a silent error message).
Thanks you everyone for all Reply
I try to understand the code and stuff but feel very lost

Anyway I have one more problem I want to make Skill unusable but visible in battle(so Player can read) any suggestion?
You can give it the Yanfly Skill Core notetag:
Code:
<Custom Requirement>
value=false;
</Custom Requirement>

Original thread: https://forums.rpgmakerweb.com/threads/passive-that-heal-mp-on-basic-attack.160155/