MV Deal Damage to 1 Random Enemy on Death

So i have a TNT Actor that is supposed to do what the title says but instead it deals damage to all enemies which is not what i want.
I made it so the TNT has a state which has the tag <Category: Bypass Death Removal> so that it's not removed on death.
So how can i make this to work like i want?
Spoiler: State 1 Effect
JavaScript:
<Custom Apply Effect>
if (target.isDead() && target.isStateAffected(26)) {
var foes = target.opponentsUnit().aliveMembers();
for (var i = 0; i < foes.length; ++i) {
var foe = foes[i];
if (foe && foe.enemyId() !== 3) {
foe.gainHp(-target.mat);
foe.startDamagePopup();
target.removeState(26);
if (foe.isDead()) {
foe.performCollapse();
}
foe.clearResult();
}
}
}
</Custom Apply Effect>
Instead of

Code:
var foe = foes[i]

do

var foe = foes[Math.floor(Math.random() * foes.length)]
@Trihan thanks,it worked as intended almost,i had to remove the looping part to fix it since it triggered the effect more than once, now it works as intended.

Spoiler: Final Result
JavaScript:
<Custom Apply Effect>
if (target.isDead() && target.isStateAffected(26)) {
var foes = target.opponentsUnit().aliveMembers();
var foe = foes[Math.floor(Math.random() * foes.length)]
if (foe && foe.enemyId() !== 3) {
foe.gainHp(-target.mat);
foe.startDamagePopup();
target.removeState(26);
if (foe.isDead()) {
foe.performCollapse();
}
foe.clearResult();
}
}
</Custom Apply Effect>
Ah sugar yeah, sorry. I meant to tell you to take out the for loop as well.
@Trihan i discovered a problem by chance, sometimes it won't deal damage at all and i'm pretty sure it has to do with this line:

if (foe && foe.enemyId() !== 3)

If it can't target enemy 3 it won't choose a new target, how can i exclude enemy with id 3 from the selection of enemies before it checks if the enemy is alive?
If i'm not wrong replace

JavaScript:
var foe = foes[Math.floor(Math.random() * foes.length)]

with

JavaScript:
let random = 0;
do{
  random = Math.floor(Math.random() * foes.length);
} while(random !== 3)
var foe = foe[random];
If i'm not wrong replace

JavaScript:
var foe = foes[Math.floor(Math.random() * foes.length)]

with

JavaScript:
let random = 0;
do{
  random = Math.floor(Math.random() * foes.length);
} while(random !== 3)
var foe = foe[random];
It freezes the game when the character dies and it doesn't get the chance to damage any enemy.
You might want to debug your code to see at which point the code is failing.

See here

If the character dies, it didn't get stuck in the do...while loop, but I don't know why it freezes with that change alone, and the code before didn't freeze. But well, something was wrong with it according to you.

So far you should have this code
Spoiler
JavaScript:
if (target.isDead() && target.isStateAffected(26)) {
  // Get all enemies
  var foes = target.opponentsUnit().aliveMembers();
  // Get random enemy
  let random = 0;
  do {
    random = Math.floor(Math.random() * foes.length);
  } while (random !== 3);
  var foe = foes[random]; // Fixed foe[random] to foes[random]

  // Process enemy damage
  if (foe) {
    foe.gainHp(-target.mat);
    foe.startDamagePopup();
    target.removeState(26);
    if (foe.isDead()) {
      foe.performCollapse();
    }
    foe.clearResult();
  }
// Should handle an error in an else scenario

}

But well I am not an expert in the battle side of MV, so wait for more help otherwise.
JavaScript:
var foe = foe[random];
This is the culprit, and it's because of a bit of poor coding practice on the part of @Johnny_Ray 😉

You don't ever want to name two variables too similarly to each other, or you run the risk of getting them confused like just happened here. Saying a brand new variable is equal to an index of itself clearly can't mean anything; this should be var foe = foes[random]

(or I would suggest naming them to be more discreet, such as target and foes)
Lol, that happens when you don't use an IDE to program, I was just editing the code here in the forum. Thanks for the fix. Edited the code in my previous post with the fix

But well that error would have been easily caught in the debugger, so it is very recommended to set it up.
If you don't want to use a while loop, you could also just remove the enemies with ID 3 from the array in the first place:

var foes = target.opponentsUnit().aliveMembers().filter(enemy => enemy.enemyId() !== 3)
Thank you everyone for the help you've given me, i can say for sure that now it functions as intended.
Glad we could help, do use the filter method that is more elegant though.

Now that I think I might have made that mistake because I was humming Iosys - FOE while writing that, lol.

Original thread: https://forums.rpgmakerweb.com/threads/deal-damage-to-1-random-enemy-on-death.134953/