Collapse effect not happening, dead bodies left visible

I have a skill which applies damage to the target and splash damage to all other enemies. That part works fine.

The problem only arises if the splash damage is enough to defeat one of these other enemies. Then we see the down sprite of the enemy and it never goes away, just clutters up the battle field until the battle ends. It looks ridiculous.

My thought was that perhaps applying damage in this way bypasses the usual way the engine calls for the collapse effect.

At the moment I have this code which applies the damage:
Code:
<JS Post-Damage>
for (let i=0; i<target.friendsUnit().aliveMembers().length; i++)
{
    if (target.friendsUnit().aliveMembers()[i]!=target)
    {
        target.friendsUnit().aliveMembers()[i].gainHp(0-Math.round(value/10));
        target.friendsUnit().aliveMembers()[i].startDamagePopup();
    }
}
</JS Post-Damage>

I tried putting it in <JS Pre-Apply> </JS Pre-Apply> in case that was when the collapse effect is called, but then it did no damage.

Does anyone have any idea of what I could do?
Just to mention it, Yanfly's Tips & Tricks are good to browse through to make note of certain tidbits. The notetags are different in MV, but almost all of the battle-related code is the same. This is in a few, like Thornmail.

After the two lines of code in your if in the middle, you'd add
Code:
if (target.friendsUnit().aliveMembers()[i].hp <= 0)
    target.friendsUnit().aliveMembers()[i].performCollapse();
@ATT_Turan
Just to be absolutely certain, it would be inserted like this, with no additional brackets or anything?
Code:
<JS Post-Damage>
for (let i=0; i<target.friendsUnit().aliveMembers().length; i++)
{
    if (target.friendsUnit().aliveMembers()[i]!=target)
    {
        target.friendsUnit().aliveMembers()[i].gainHp(0-Math.round(value/10));
        target.friendsUnit().aliveMembers()[i].startDamagePopup();
        if (target.friendsUnit().aliveMembers()[i].hp <= 0)
        target.friendsUnit().aliveMembers()[i].performCollapse();

    }
}
</JS Post-Damage>
Just to be absolutely certain, it would be inserted like this, with no additional brackets or anything?
fastest way to see is to run it if you can and see if it works
if youre only running one command after an IF statement, then there is no need to set up braces.
@Robro33 It's actually quite a difficult thing to playtest - getting an enemy just weak enough for splash to kill it. I've done maybe 15 playtests with this formula and never got to the correct circumstances. This is the reason why the problem only emerged long after the skill was implemented. The code applies the splash damage correctly, I shall just have to hope that the collapse effect occurs.
It's actually quite a difficult thing to playtest - getting an enemy just weak enough for splash to kill it. I've done maybe 15 playtests with this formula and never got to the correct circumstances
difficult to discover, sure. but you know the steps to get to the issue and a possible solution to apply.

it wouldnt take long to make a troop that has enemies weak enough to be 1shot by the splash damage or use a troop event in an existing troop to set some enemy's hp to 1 on turn 0 so that you can observe what happens when they die to the splash damage

if it works, then it works and you would have known it does faster than it takes for someone else to respond
@Robro33 It's actually quite a difficult thing to playtest - getting an enemy just weak enough for splash to kill it.
Why? Make your enemy have 1 HP while you're testing.
After a bit of work and testing I came up with this as a working solution:

Code:
<JS Pre-Damage>
for (var i=0; i<target.friendsUnit()._enemies.length; i++);
{
     if (target.friendsUnit()._enemies[i]!=target);
    {
        if (target.friendsUnit()._enemies[i].hp !== 0) {
            target.friendsUnit()._enemies[i].gainHp(0-Math.round(value/10));
            target.friendsUnit()._enemies[i].startDamagePopup();
            if (target.friendsUnit()._enemies[i].hp <= 0);
            target.friendsUnit()._enemies[i].performCollapse();
        }
    }
}
</JS Pre-Damage>

There was several issues using Post-Damage and while the solution @ATT_Turan posted above helped, there was an odd issue with the skill still not properly collapsing all enemies and also for some reason the default code @Kes posted above was only able to target half of the remaining enemies in battle per attack.

Had to also add in a check using this setup to not hit already dead enemies since it was no longer only looking at "aliveMembers()" like before.

Original thread: https://forums.rpgmakerweb.com/threads/collapse-effect-not-happening-dead-bodies-left-visible.170545/