have enemy stay on 10% hp no matter how strong attack is to trigger troop event

not sure if this script is right, but im trying to make a troop event, for if the enemy reaches 10% of hp then the event happends and the battle is aborted, the thing is, it works like 50% of the time, sometimes the battle is aborted but other times the battle still continues, so dunno if the event needs fixing, this is the script i used

var enemyId = 1; // Replace 1 with the actual ID of your enemy
var thresholdPercentage = 10; // The desired threshold percentage

// Get the enemy object
var enemy = $gameTroop.members()[enemyId - 1];

// Calculate the minimum HP based on the threshold percentage
var minHp = (enemy.mhp * thresholdPercentage) / 100;

// Check if the current HP is below the minimum
if (enemy.hp < minHp) {
// Set the HP to the minimum
enemy.setHp(minHp);
}

// Now the enemy's HP will not drop below 10%


so i dont know why half the time the event doesnt abort the battle, i dont know if its the damage dots or an attack is stronger than 10% of enemy damage, it just doesnt want to work

i want it so any attack will not make the enemy drop bellow 10% hp no matter how stong the attack is, that way i can abort the battle
I could use immortal state but i want a more realistic approach, when your hp reaches 0 ur dead, so i dont want the enemy hp to reach 0, it needs to be 10% and cannot lose anymore hp no matter what
image.png
image.png
The issue is that if an attack kills the last living enemy, any battle events that depend on HP percentage don't fire. Usually we use an immortal flag for cases like this, and handle "killing" the enemy manually in the event.
its only one enemy though
its only one enemy though
Yes, that is literally my point. If you deal damage to the enemy that will kill it, your 10% event won't run.
so i cant have the enemy hp stop at 10% i have to give it immortal?
the script i have, wont event kill the enemy either, the battle will just go on until the player is dead since the enemy will never die regardless
Using the Immortal state is the best way to achieve what you want without possible issues arising.
so i cant have the enemy hp stop at 10% i have to give it immortal?
the script i have, wont event kill the enemy either, the battle will just go on until the player is dead since the enemy will never die regardless
You give the enemy "immortal" (or State resist: KO), and do your 10% HP event as normal.

Then when you actually want your enemy to "die", do an event with whatever condition that removes the immortal trait/flag from it.
Also, just to put it out there, this:
Code:
var enemyId = 1; // Replace 1 with the actual ID of your enemy
// Get the enemy object
var enemy = $gameTroop.members()[enemyId - 1];
is completely wrong. The enemy ID has nothing to do with its index in the troop.
Also even if it did, setting a variable to 1 and then using it to pass that value - 1 as an array index is a bit redundant; it'd be functionally identical to just putting [0] to begin with.

(also also if the troop event is triggered by that specific enemy having 10% HP or less, then there's no point in a script that checks whether it has less than or equal to 10% HP; if it didn't, the event wouldn't be running)
yeah, but i dont want the enemy to reach 0% hp at all, the enemy has to stop at 10% or any other low % if it cant be 10%, then a bit of dialogue and abort battle, thats all it needs to do
yeah, but i dont want the enemy to reach 0% hp at all, the enemy has to stop at 10% or any other low % if it cant be 10%, then a bit of dialogue and abort battle, thats all it needs to do
So give it the immortal flag, do your troop event activated by the enemy being at 10%, then in that event do your dialogue and abort battle command. (and if you absolutely need to make sure the HP stays at 10% visually, then just set its HP to 10% of its max here too)

I'm genuinely struggling to comprehend what's left here that you're unsure on.
have a running check for every attack, and divert the attack elsewhere.
if the result of that attack is going to kill you, then you trigger.

set the trigger to switch ON, span battle.
set the check to turn 0 +1, span moment.
the HP > 10% condition, you validate with your secondary check, not the troop event.
set the check to turn 0 +1, span moment.
and 10% hp condition

i think that might have worked, i have to battle over and over again to see if it ends in the same result all the time
have a running check for every attack, and divert the attack elsewhere.
if the result of that attack is going to kill you, then you trigger.

set the trigger to switch ON, span battle.
set the check to turn 0 +1, span moment.
the HP > 10% condition, you validate with your secondary check, not the troop event.
have a running check for every attack, and divert the attack elsewhere.
if the result of that attack is going to kill you, then you trigger.

set the trigger to switch ON, span battle.
set the check to turn 0 +1, span moment.
the HP > 10% condition, you validate with your secondary check, not the troop event.
This is certainly a valid approach to take, but I fail to see how it's preferable to using the HP % condition that already exists for troop events. As I understand it, OP simply needs some dialogue to play when the enemy hits 10% HP and then abort the battle. Why would a secondary check be needed?
This is certainly a valid approach to take, but I fail to see how it's preferable to using the HP % condition that already exists for troop events. As I understand it, OP simply needs some dialogue to play when the enemy hits 10% HP and then abort the battle. Why would a secondary check be needed?
Because OP can't seem to wrap their mind around the idea that just using the Immortal flag will easily do what's necessary, focusing way too hard on wanting enemy HP at exactly 10%.
Why would a secondary check be needed?
because *IF* you can't control how much damage the attack is going to do, and *IF* the battle is not press-turn or tactical, then you need to monitor the HP at each fraction increment of the turn so that *when* the HP gets to below 10%, it triggers the sequence.
not at the end of a whole turn that may cause the enemy to be overwhelmed by damage that you can't stop, depleting the HP bar that maybe you can too see, making the dialogue misplaced when the enemy talks back at 0 HP.

I have the branch off of this scenario in a battle, where I need the battle to end "now" after a dialogue, which happens after either turn X, or the enemy at Y HP, but the dialogue must play, and the player gets a last hit... so I make the enemy 1 HP.
same process, different check.

Original thread: https://forums.rpgmakerweb.com/threads/have-enemy-stay-on-10-hp-no-matter-how-strong-attack-is-to-trigger-troop-event.165760/