how to add the condition: "if there is only one enemy"

I have added an ability to my hero, for now I have managed to make it work, but I need this to be performed only if there is 1 enemy in battle, and if the enemy has low HP (maybe 30% or less) below I show how it works:
imagen_2021-04-05_203612.png

(sorry for my english)

In short, I want the ability to be successful if there is only 1 enemy, and if it has <30% of its hp
$gameTroop.aliveMembers().length will give you the number of enemies, and
Code:
if ($gameTroop.aliveMembers()[0].hp / $gameTroop.aliveMembers()[0].mhp < .3)

You may want to change your existing code to reference aliveMembers as well.
if ($gameTroop.members()[0].hp / $gameTroop.members()[0].mhp < .3)

or

Code:
$gameTroop.members().length == 1 && $gameTroop.members()[0].hpRate() < 0.3
Code:
$gameTroop.members().length == 1 && $gameTroop.members()[0].hpRate() < 0.3
I haven't used them for anything, so I always forget the parameters have percentage functions built in.

But wouldn't one want aliveMembers? I thought members didn't change size as you killed stuff, am I wrong?
or

Code:
$gameTroop.members().length == 1 && $gameTroop.members()[0].hpRate() < 0.3
thanks to both of you!
now the skill works correctly!
But wouldn't one want aliveMembers?

The OP only said "one enemy" - it wasn't clear whether that meant only one enemy still alive, though there could have been others initially, or whether it meant only one enemy to begin with.

OP - if you mean only one enemy remaining, use $gameTroop.aliveMembers().length instead of $gameTroop.members().length because the latter will count defeated enemies.
The OP only said "one enemy" - it wasn't clear whether that meant only one enemy still alive, though there could have been others initially, or whether it meant only one enemy to begin with.

OP - if you mean only one enemy remaining, use $gameTroop.aliveMembers().length instead of $gameTroop.members().length because the latter will count defeated enemies.
I have used $gameTroop.aliveMembers().length , effectively I wanted only 1 live enemy. THANKS!
In that case, as I mentioned above, make sure to also change that in your original script, where you check the enemy's name - otherwise it will always be asking "was the first member of the troop a slime," rather than "is the only remaining enemy a slime?"

Also, unless you do something in your game with changing the enemy's name, there's an easier way to do that. You can simply do:
Code:
if ($gameTroop.aliveMembers()[0].enemyId()==X)
where X is the id of the blue slime in your database. That way it's one check, no need for a variable (although you also don't need the variable to check the name...)

Original thread: https://forums.rpgmakerweb.com/threads/how-to-add-the-condition-if-there-is-only-one-enemy.135196/