Average Damage to All Enemies equal to a Variable

@Trihan - you may need another tag (as well?): <custom execution> appears to run once, before the action is invoked on any target, and does not offer a value variable.

Also, the core script BattleManager.updateAction shifts the next target out of BattleManager._targets prior to invoke/apply, so the current target is not present in _targets during invocation. I haven't checked to see if/how Yanfly's stuff changes that.

I'm thinking something like this may work (untested):
Spoiler: tags
JavaScript:
<Post-Damage Eval>
// --- do once per target --- //
// count hits (lazy init)
user._hitCount || (user._hitCount = 0);
++user._hitCount;
// count damage (lazy init)
user._totalDamage || (user._totalDamage = 0);
user._totalDamage += value;
</Post-Damage Eval>

<After Eval>
// --- do once per action --- //
// heal only if hit for some damage
if (user._hitCount && user._totalDamage) {
  // get average damage
  const healing = Math.floor(user._totalDamage / user._hitCount);
  // heal all living friends of user
  for (const m of user.friendsUnit().aliveMembers())
    m.gainHp(healing);
}
// clean up
delete user._totalDamage;
delete user._hitCount;
</Post-Damage Eval>
That might work, but you've forgotten to change post-damage to after in your second closing tag.
Oops, thanks for pointing that out! I'll go edit it~
Oops, thanks for pointing that out! I'll go edit it~
Awesome! Thanks, guys! Standing by for the corrected script 🙂
Did you try what Trihan advised first: replacing targets with BattleManager._targets?

Otherwise, I edited my post like, 1 minute after saying I would edit it:
I'm thinking something like this may work (untested):
Spoiler: tags
JavaScript:
<Post-Damage Eval>
// --- do once per target --- //
// count hits (lazy init)
user._hitCount || (user._hitCount = 0);
++user._hitCount;
// count damage (lazy init)
user._totalDamage || (user._totalDamage = 0);
user._totalDamage += value;
</Post-Damage Eval>

<After Eval>
// --- do once per action --- //
// heal only if hit for some damage
if (user._hitCount && user._totalDamage) {
  // get average damage
  const healing = Math.floor(user._totalDamage / user._hitCount);
  // heal all living friends of user
  for (const m of user.friendsUnit().aliveMembers())
    m.gainHp(healing);
}
// clean up
delete user._totalDamage;
delete user._hitCount;
</After Eval>
[Edit: corrected 2nd </Post-Damage Eval> to </After Eval>.]
Just click the spoiler to see inside. 👋

(My suggestion, like Trihan's, requires YEP Skill Core.)
Did you try what Trihan advised first: replacing targets with BattleManager._targets?

Otherwise, I edited my post like, 1 minute after saying I would edit it:

Just click the spoiler to see inside. 👋

(My suggestion, like Trihan's, requires YEP Skill Core.)
1. No, I did not know which "targets" they were saying to replace. It is in the script I provided x5 times. Sorry for the confusion.

2. Yes, I have YEP_SkillCore.

3. I tried your code and did not receive any console errors this time. However, the attack did damage to the enemy but did not heal the party.

Edit: I removed their code before trying yours, and only left the action sequence.
Ah, "targets" is invalid for the set of targets of the skill. Try changing that to

BattleManager._targets
Please forgive the double post. Which "targets" line of code am I replacing?
Please forgive the double post. Which "targets" line of code am I replacing?
Both places it says "targets" as a single word.
Both places it says "targets" as a single word.
Here is the edited script I used below:

JavaScript:
<custom execution>
user._totalDamage = user._totalDamage || 0;
user._totalDamage += value;
if (user._targetsHit === BattleManager._targets.length - 1) {
  let healing = Math.floor(user._totalDamage / BattleManager._targets.length);
  $gameParty.aliveMembers().forEach(member => member.gainHp(healing));
  delete user._totalDamage;
  delete user._targetsHit;
} else {
  user._targetsHit++;
}
</custom execution>

<setup action>
display action
immortal: targets, true
perform start
wait for movement
cast animation
wait for animation
</setup action>

<target action>
jump user: 200, 20
move user: target, front center, 20
wait for movement
motion swing: user
animation 181: target
wait for animation
action effect
animation 43: target
wait for animation
</target Action>

<Follow Action>
remove state 17: existing actors
</Follow Action>

I had the same result as before, except for no console errors this time. The enemy received damage, but the party was not healed.
OK, I just tested and this worked for me, with YEP_SkillCore and no other plugins:
Spoiler: with only skill core
JavaScript:
<Post-Damage Eval>
// --- runs once per hit --- //
// count hits (lazy init)
user._hitCount || (user._hitCount = 0);
++user._hitCount;
// count damage (lazy init)
user._totalDamage || (user._totalDamage = 0);
user._totalDamage += value;
</Post-Damage Eval>

<After Eval>
// --- runs once per target --- //
// if this was the last target
if (!BattleManager._targets.length) {
  // heal only if hit for some damage
  if (user._hitCount && user._totalDamage) {
    // get average damage
    const healing = Math.floor(user._totalDamage / user._hitCount);
    // heal all living friends of user
    for (const m of user.friendsUnit().aliveMembers()) {
      m.gainHp(healing);
      m.startDamagePopup();
    }
  }
  // clean up
  delete user._totalDamage;
  delete user._hitCount;
}
</After Eval>
My previous suggestion was healing once per target (I thought Before/After Eval ran once per action but it's actually once per target). Also, it was healing everyone but the popup only showed up on the skill user, so I added a startDamagePopup call.

Then I tried with YEP_BattleEngineCore and the healing completely failed to trigger. (I'm guessing it's related to how BEC handles multiple targets.) It started working when I replaced this line:

if (!BattleManager._targets.length) {
...with this:

if (target === BattleManager._allTargets[BattleManager._allTargets.length - 1]) {
The _allTargets thing is added by BEC.

[Edit: moved "clean up" section inside the "if this was the last target" branch.]
Awesome! It works! Thank you all! Solved.
Great! I did notice a small mistake regarding the "clean up" section: it was getting called every target, instead of only on the last target, meaning the heal was always equal to the most recent target's damage, rather than the average over all targets. I've edited my previous post to fix that, should be OK now. 👋

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/average-damage-to-all-enemies-equal-to-a-variable.153085/