MV Help making actor gain MP when taking damage

Hi, I need help figuring out the script for an actor in my game that gains mana when taking damage.
I need to check for that specific actor (not any actor) being attacked, and then gain mana if they take damage, gaining more if it was a critical hit.

I currently have this on a base troop event:

var actor = $gameActors.actor(4);
if (b = actor) {
b.gainMp(1);
}

However, that doesn't seem to work very well, since apparently it can't trigger more than once per actor turn, and I also couldn't figure out how to use result.critical() and result().missed inside a troop event.

I'm using basically all of Yanfly's plugins, which I assume would let me add this functionality directly into the actor's notetags, but I'm not sure how to do that.
I'm using basically all of Yanfly's plugins, which I assume would let me add this functionality directly into the actor's notetags, but I'm not sure how to do that.
If you google using the words you used in the title, a few threads about how to do it come up.

you'd start with this:
then give the state to your battlers (easiest by making it a passive with yanfly's passive state plugin), adjust the mp gain from it, and check if target.result().critical is true so you can give extra MP on a crit.
Huh, I searched quite a bit earlier and that particular post didn't show up. Maybe I wasn't wording my search properly.

Anyway, I got it working now. Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/help-making-actor-gain-mp-when-taking-damage.179873/