MV Trying to fix "on death" state effect using Yanfly Buffs/States core

Hey all,

In my game I have this state I made that checks what element is dealing the killing blow to the enemy. It looks like this:
JavaScript:
<Custom Deselect Effect>
var lethal = 1;
var nonlethal = 2;
var diplomacy = 3;
var xp = target.def;
var basexpa = $gameVariables.value(1);
var basexpb = $gameVariables.value(2);
var basexpc = $gameVariables.value(3);
if (this.isHpEffect() && target.hp == 0 && this.item().damage.elementId === lethal) {
  target.startAnimation(65);
  $gameVariables.setValue(1, basexpa +xp);
}
if (this.isHpEffect() && target.hp == 0 && this.item().damage.elementId === nonlethal) {
  target.startAnimation(60);
  $gameVariables.setValue(2, basexpb +xp);
}
if (this.isHpEffect() && target.hp == 0 && this.item().damage.elementId === diplomacy) {
  $gameVariables.setValue(3, basexpc +xp);
}
</Custom Deselect Effect>

I apologize for the ugly code. Probably a lot of unnecessary stuff in there. Anyway, this works in most cases. The problem is when I use an attack that has repeats in it. So if I have an attack that hits 10 times, and the target dies after the first hit, this will run every single time.

I've tried using target.isDead() and target.isAlive() but those don't seem to be recognized while an attack is repeating. What I think I need is something that checks if the attack is changing the target's HP (since it's stuck at 0, right?), but I don't know how to check that.

I don't think the other Yanfly notetags will work. They seem to run with the wrong timing, or something like "on removal" doesn't seem to work in the case of death.
Make a Custom React Effect, which happens before damage is dealt:
Code:
// Does the target have HP, and will the attack kill it?
if (this.isHpEffect() && target.hp>0 && value>=target.hp)
    target.wasKilled=true;

Then your conditionals inside the Deselect Effect say:
Code:
if (target.wasKilled && this.item().damage.elementId === lethal)
{
    target.startAnimation(65);
    $gameVariables.setValue(1, basexpa +xp);
    target.wasKilled=false;
}
etc.

That way, only the first hit will be true, all the rest will see that wasKilled is false and won't execute the code again.
Wow, this is such a smart way to use this plugin! I guess I just didn't think I could use more than one of those at a time? Even though I've probably done that somewhere in this or another project, derp. I also didn't think to declare a variable in one tag and use it in another, even though I've definitely done that :hswt:

Anyway, this is genius. You're a genius @ATT_Turan ! This is also a great bit of code for anyone looking to make some "on death" type effects without having to manually event stuff in the troop window. Just gotta put in another check for an immortal state if your game uses it (mine might not).

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-fix-on-death-state-effect-using-yanfly-buffs-states-core.151742/