Death Nova buff modification

Yanfly has shown how to make Guild War's Death Nova state in rpg maker mv, it will aways Works on the target's oponente no matter what and i wanted it to affect the entire enemy team when the target dies if the target is also a enemy.

// Death Nova
<Custom Apply Effect>
// Get the Death Nova state's ID
var deathNovaId = 271;
// Check if the target is dead and is affected by the Death Nova state
if (target.isDead() && target.isStateAffected(deathNovaId)) {
// Get the poison state
var poisonId = 37;
// Get the original caster for the Death Nova state
var caster = target.stateOrigin(deathNovaId);
// Calculate the damage dealt
var dmg = caster.mat * 6;
// Get the dying unit's opponents
var foes = target.opponentsUnit().aliveMembers();
// Loop through the foes
for (var i = 0; i < foes.length; ++i) {
// Get the currently looped foe
var foe = foes;
// Check if the foe exists
if (foe) {
// Make the foe take damage.
foe.gainHp(-dmg);
// Add the poison state to the foe.
foe.addState(poisonId);
// Show the damage popup.
foe.startDamagePopup();
// Check if the foe is dead
if (foe.isDead()) {
// If it is, collapse the foe
foe.performCollapse();
}
// Clear the foe's results
foe.clearResult();
}
}
// Play an animation on the dying unit
target.startAnimation(109);
// Remove the Death Nova state
target.removeState(deathNovaId);
}
</Custom Apply Effect>

Original thread: https://forums.rpgmakerweb.com/threads/death-nova-buff-modification.87302/