Help with dead enemies in troop

Hello, I'm back after a month of hiatus (had to do other stuff for university...). Now I have about a month free, so here I am again, full of questions and troubles.
So, surely no one remembers, but a few months ago I asked here how to create a skill that deals damage to both enemies and yourself. Today I was testing it and SURPRISE, it doesn't work properly. Basically, the skill does two things: it deals damage and increases TP to the enemy (which is a BAD thing in my game, TP should be as low as possible). When you deal enough damage to kill an enemy, though, I noticed that the index that I'm using to reference enemies is advancing automatically (e.g.: I have two bats, Index 0 and 1 respectively, but 0 dies from the damage, so Index becomes 1 immediately, without returning to the loop... at least that's what I figured?). The problem is that later in the code, I check if the enemy died from the attack and perform collpase on him. But since the index increases, this won't happen. I'll post my code for clarity.

Code:
<Custom Regenerate Effect>
user.gainHp(-Math.ceil(user.atk));
for (i=0;i<$gameTroop.members().length;i++){
 if($gameTroop.members()[i].isAlive()){
  currentAttack= (user.atk*2-$gameTroop.members()[i].def)*$gameTroop.members()[i].elementRate(5);
  $gameTroop.members()[i].gainTp(Math.ceil(currentAttack*10/100));
  $gameTroop.members()[i].gainHp(-Math.ceil(currentAttack));
  if($gameTroop.members()[i].isDead()){
   $gameTroop.members()[i].performCollapse();
}
}}
</Custom Regenerate Effect>

So the question is: since apparently members() doesn't include dead enemeis, is there some function that includes both alive and dead troop members? I tried also allMembers, but no luck. In case that doesn't exist (or there is something else I'm not considering... which is likely, since I'm super rusty), how else can I fix this?

Thanks in advance.
Im still confused on the goal. You said it was a skill that targets an enemy, hurts the user, and adds TP to the target, but you provided a state note tag. As a skill it would be an easy thing to do, but youre doing it as a state as damage over time against all enemies?
So the question is: since apparently members() doesn't include dead enemeis, is there some function that includes both alive and dead troop members? I tried also allMembers, but no luck. In case that doesn't exist (or there is something else I'm not considering... which is likely, since I'm super rusty), how else can I fix this?
When an enemy dies, the indices of the remaining enemies dont shift to replace the one thats dead. $gameTroop.members() would return all members. if you needed alive or dead members, you'd use aliveMembers() and deadMembers() respectively. A dead battler by default cannot have states other than KO, and it seems that the effect you're trying to do is through a state?

It might be best to try to re-explain. Are you trying to make a skill that harms the user, deals damage to one target, and increases the target's tp? or is supposed to be some recurring state effect applied by a skill?
Like Robro says, by default $gameTroop.members() includes all enemies, alive and dead. Just like $gameParty.members() includes living and dead actors. If you're seeing something different then maybe check your plugins.

You could try something like this in whatever tag you decide to use:
JavaScript:
user.gainHp(-Math.ceil(user.atk));
for (var foe of user.opponentsUnit().aliveMembers()){
  var dmg = (user.atk * 2 - foe.def) * foe.elementRate(5);
  foe.gainTp(Math.ceil(dmg / 10));
  foe.gainHp(-Math.ceil(dmg));
  if (foe.isDead())
    foe.performCollapse();
}
for...of creates an iteration range first, so any changes to that range mid-loop should not affect which values get looped over.

[Edit: added var to line dmg = ..., so that it's declared locally rather than globally.]
Like Robro says, by default $gameTroop.members() includes all enemies, alive and dead. Just like $gameParty.members() includes living and dead actors. If you're seeing something different then maybe check your plugins.

You could try something like this in whatever tag you decide to use:
JavaScript:
user.gainHp(-Math.ceil(user.atk));
for (var foe of user.opponentsUnit().aliveMembers()){
  dmg = (user.atk * 2 - foe.def) * foe.elementRate(5);
  foe.gainTp(Math.ceil(dmg / 10));
  foe.gainHp(-Math.ceil(dmg));
  if (foe.isDead())
    foe.performCollapse();
}
for...of creates an iteration range first, so any changes to that range mid-loop should not affect which values get looped over.

Mh, so it could be a plugin issue? I'll look into it, in the meantime, I will try your solution!
EDIT: It worked, thanks! I'll still try to look into that problem regardless, but at least the skill should work now.
The other thing that may help is adding an HP check to your conditional.

isDead() only checks for the presence of the death state, and if you're doing it in the same sequence of code that changed their HP, that might not have been applied yet.

Your original code may work if you change the condition to:
Code:
if ($gameTroop.members()[i].isDead() || $gameTroop.members()[i].hp<=0)

As a style tip (it doesn't affect your code), it is common practice to have a space after "if" before its parentheses to visually differentiate it from a function.
The other thing that may help is adding an HP check to your conditional.

isDead() only checks for the presence of the death state, and if you're doing it in the same sequence of code that changed their HP, that might not have been applied yet.

Your original code may work if you change the condition to:
Code:
if ($gameTroop.members()[i].isDead() || $gameTroop.members()[i].hp<=0)

As a style tip (it doesn't affect your code), it is common practice to have a space after "if" before its parentheses to visually differentiate it from a function.
Oh, you are right! I imagined it checked hp too, but it makes perfect sense. As for the style tip, yeah my code is very messy and this tip is very useful ahahah

Original thread: https://forums.rpgmakerweb.com/threads/help-with-dead-enemies-in-troop.160892/