Triggering the gaining of TP on critical hit.

I'm doing this as a universal passive state using Yanflys Buffs and States core and Passive States, I've tried several variations but I'm messing up somewhere.

<Custom Establish Effect>

if (this.isPhysical() && target.result().hpDamage > 0 && target.result().critical){



//If user TP is over 0.
if (user.tp >= 0){
/Calculate TP gain.
var value = Math.floor(user.tp * 2 * 0.25);
user.gainTP(value);
} else {
//If TP is at 0 add 25 TP.
var value = 25;
user.gainTP(value);

}



// Check if target is alive.

if (target.isAlive()) {

// Remove 25 TP from target.
if (target.tp >= 0){
var value = 25;
target.gainTP(-value);

}
}
}

</Custom Establish Effect>
this is mine that works perfect (this is in the State):


<Custom Establish Effect>

// Check if the attack is a physical attack that dealt HP damage from a critical hit.

if (this.isPhysical() && target.result().hpDamage > 0 && target.result().critical) {

user.gainTp(5);

}

</Custom Establish Effect>

It does exactly as your title asks, gaining additional TP when dealing a critical strike. by default in my game, attacking gives you 5 TP, so this passive state effectively means you get double TP when you deal a critical.

Original thread: https://forums.rpgmakerweb.com/threads/triggering-the-gaining-of-tp-on-critical-hit.118660/