Chance of activating Guts effect

As we all know, there's multiple ways of creating an "Endure" state in RPG Maker MV.

For those of you who don't know, an "Endure" or "Guts" state is a state where upon taking fatal damage, a character is revived or holds on with minimal HP. I'm using Yanfly's second chance code to replicate this in my game, however, I want to add an extra line of code that will give this effect a chance to occur depending on the characters luck stat.

I've been having trouble accomplishing this however, and would like some help.

Here are the endure codes:

<Custom React Effect>
// Check to see if the party is in battle.
if ($gameParty.inBattle()) {
// Sets the flag if the target has more than 1 HP at the time of death.
target._secondChance = target.hp > 1;
}
</Custom React Effect>

<Custom Respond Effect>
// Check to see if the party is in battle, has the Second Chance flag, and if the target is dead with 0 HP.
if ($gameParty.inBattle() && target._secondChance && target.hp <= 0) {
// Play the revival animation.
target.startAnimation(49);
// Set the target's HP to 1.
target.setHp(1);
}
</Custom Respond Effect>

<Category: Bypass Death Removal>

<Custom Deselect Effect>
// Check if the target's HP is 0 or is currently in the death state.
if (target.hp <= 0 || target.isDead()) {
// Play the auto-life animation on target.
target.startAnimation(49);
// Set the amount of HP recovered to 10%.
var rate = 0.10;
// Calculate the HP healed.
var heal = Math.floor(target.mhp * rate);
// Remove the Auto-Life state.
target.removeState(stateId);
// Heal the target.
target.gainHp(heal);
// Make the damage popup show for the heal.
target.startDamagePopup();
// Clear the target's results.
target.clearResult();
}
</Custom Deselect Effect>

Basically, I want to add a formula to calculate the chance of this effect happening. Something like if = a.luk / 2 . That way, a character with a high luck chance will have a better chance of activating the endure effect in battle.

Thank you for reading through my thread!
You can do this pretty easily by just adding code on one line:

Code:
<Custom React Effect>
// Check to see if the party is in battle.
if ($gameParty.inBattle()) {
// Sets the flag if the target has more than 1 HP at the time of death.
target._secondChance = target.hp > 1  && Math.randomInt(100) < target.luk / 2; //Note changes
}
</Custom React Effect>

<Custom Respond Effect>
// Check to see if the party is in battle, has the Second Chance flag, and if the target is dead with 0 HP.
if ($gameParty.inBattle() && target._secondChance && target.hp <= 0) {
// Play the revival animation.
target.startAnimation(49);
// Set the target's HP to 1.
target.setHp(1);
}
</Custom Respond Effect>

You'd have to find a good randomInt value to work within your game, but the logic is sound.
So basically, (just to make sure I understand) this will generate a random number between 1 and 100. If the luck stat divided by two is more than that number, the second chance effect will activate?
technically, a number from 0 and 99, but yes for the rest.
It might be better to make two random calls.

Code:
target._secondChance = target.hp > 1  && Math.randomInt(100) < Math.randomInt(target.luk);

Something along those lines.
Okay, I'll test this when I get a chance and get back to you.

Original thread: https://forums.rpgmakerweb.com/threads/chance-of-activating-guts-effect.86962/