MV Apply buff when the user exploits weakness/lands a crit?

Hello there!
So I'm attempting to make a custom passive state with the above-mentioned effect. However, it appears none of my code actually works, and after many iterations, I eventually had to come here for help as nothing I tried worked.

Spoiler: code
JavaScript:
<Custom Establish Effect>
if (this.isHpEffect() && value > 0) {
  var elements = this.getItemElements();
  for (var i = 0; i < elements.length; ++i) {
    var elementId = elements[i];
    if (target.elementRate(elementId) > 1.00) {
      $gameParty.members()[$gameActors.actor(13).index()].addBuff(Math.randomInt(8), 3);
      break;
    }
  }
}
if (this.result().critical) {
  $gameParty.members()[$gameActors.actor(13).index()].addBuff(Math.randomInt(8), 3);
}
</Custom Establish Effect>

You may notice that the code is specifically trying to apply the buff to actor 13, originally it was "user.addBuff", but that didn't work so in my follow-up attempts while referencing the script call doc I used that actor as the target since they were the one that learns this ability naturally.

but after many changes it still didn't work so I'm hoping someone here will be able to understand where I went wrong.

I've moved this thread to Plugin Support (from MV Support). Thank you.

I haven't tested, but I don't see anything obviously wrong with that. user should work, too.

Double-check your plugin load order. Yanfly's plugins have a recommended order, listed here:
getItemElements requires YEP Element Core. If you're not using that, try replacing the first if block with this:
JavaScript:
if (this.isHpEffect() && value > 0 && this.calcElementRate(target) > 1) {
  user.addBuff(Math.randomInt(8), 3);
}
I think this'll work with Element Core, too, so long as you're using a compatible Multi-Element Ruling. E.g. Multiply, Highest, and Average cannot be > 1 unless at least one of their inputs is > 1.

You could try adding debug logs in your tag to see if it's actually running, and if so what parts are getting skipped. E.g.
Spoiler: example
<Custom Establish Effect> console.log("<Custom Establish Effect>"); var elements = this.getItemElements(); console.log("element IDs are", elements); for (var elementId of elements) { var elementRate = target.elementRate(elementId); console.log("target's rate for element", elementId, "is", elementRate); if (elementRate > 1) { console.log("buffing from elemental weakness!"); user.addBuff(Math.randomInt(8), 3); break; } } if (this.result().critical) { console.log("buffing from crit!"); user.addBuff(Math.randomInt(8), 3); } console.log("</Custom Establish Effect>"); </Custom Establish Effect>
console.log outputs messages to the Console tab of the dev tools window (opened by pressing F8 or F12 during playtest).
Moved to: Plugin Support (from MV Support) I haven't tested, but I don't see anything obviously wrong with that. user should work, too.

Double-check your plugin load order. Yanfly's plugins have a recommended order, listed here:
getItemElements requires YEP Element Core. If you're not using that, try replacing the first if block with this:
JavaScript:
if (this.isHpEffect() && value > 0 && this.calcElementRate(target) > 1) {
  user.addBuff(Math.randomInt(8), 3);
}
I think this'll work with Element Core, too, so long as you're using a compatible Multi-Element Ruling. E.g. Multiply, Highest, and Average cannot be > 1 unless at least one of their inputs is > 1.

You could try adding debug logs in your tag to see if it's actually running, and if so what parts are getting skipped. E.g.
Spoiler: example
<Custom Establish Effect>
console.log("<Custom Establish Effect>");
var elements = this.getItemElements();
console.log("element IDs are", elements);
for (var elementId of elements) {
var elementRate = target.elementRate(elementId);
console.log("target's rate for element", elementId, "is", elementRate);
if (elementRate > 1) {
console.log("buffing from elemental weakness!");
user.addBuff(Math.randomInt(8), 3);
break;
}
}
if (this.result().critical) {
console.log("buffing from crit!");
user.addBuff(Math.randomInt(8), 3);
}
console.log("</Custom Establish Effect>");
</Custom Establish Effect>
console.log outputs messages to the Console tab of the dev tools window (opened by pressing F8 or F12 during playtest).
Thank you for the resposne! I actually managed to find the issue thanks to you, i checked the log after adding your console.log code and i noticed that I was actually receiving an error, the issue was "this.result().critical" the correct code was "target.result().critical" since apparently "this" isn't valid in that use case. Thank you again for the help!
This thread is being closed due to being solved. If you need it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/apply-buff-when-the-user-exploits-weakness-lands-a-crit.167137/