MV Skill that heals ally with least HP Percentage

As title says.

For my game, I wanted to have an attack that has an additional effect of healing an ally with the lowest health percentage. I already found code for YEP that heals a random ally after an attack, but I need one for the ally with the lowest HP percentage (or random one if there are more than one who are). What's the code I need to change that?
you could do this to get the ally with the lowest hp% and then heal them from there.
Code:
user.friendsUnit().aliveMembers().reduce((a, b) => a.hpRate() < b.hpRate()? a : b)

the likelihood of 2 battlers with the same hpRate() at any point outside of both being topped off or dead is pretty low though all things considered. above will choose the entry with the greater index in the event of a tie anywhere
you could do this to get the ally with the lowest hp% and then heal them from there.
Code:
user.friendsUnit().aliveMembers().reduce((a, b) => a.hpRate() < b.hpRate()? a : b)

the likelihood of 2 battlers with the same hpRate() at any point outside of both being topped off or dead is pretty low though all things considered. above will choose the entry with the greater index in the event of a tie anywhere
IMO It's also good to design the skill with the appropriate target scope so it doesn't like, allow you to pick an ally to cast the skill with before the skill gets redirected. I'd probably use a "None" scope here then use some clever scripting.

The best solution would probably to design a completely new scope that targets only that specific ally and allows the player to confirm the decision. (I don't immediately have a script that performs that function though)
IMO It's also good to design the skill with the appropriate target scope so it doesn't like, allow you to pick an ally to cast the skill with before the skill gets redirected. I'd probably use a "None" scope here then use some clever scripting.
They did say its an additional effect, so the skill presumably does something else entirely, but also automatically heals the lowest hp% ally. it (afaik) doesnt have to target that ally.

The best solution would probably to design a completely new scope that targets only that specific ally and allows the player to confirm the decision. (I don't immediately have a script that performs that function though)
its doable with yanfly's selection control plugin if that helps
Code:
<Custom Select Condition>
condition = target.hpRate() == user.friendsUnit().aliveMembers().reduce((a, b) => a.hpRate() < b.hpRate()? a : b).hpRate();
</Custom Select Condition>
now the skill only targets allies with the lowest hpRate(), and if multiple share, then you can choose which
the likelihood of 2 battlers with the same hpRate() at any point outside of both being topped off or dead is pretty low though all things considered. above will choose the entry with the greater index in the event of a tie anywhere
Very true. Just to be extra, for the sake of what OP requested:
Code:
let lowest = 1;
user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate()));
let targets = user.friendsUnit().aliveMembers().filter(actor => actor.hpRate() == lowest);
targets[Math.randomInt(targets.length)].gainHp(X);
where X is the amount to be healed.

Edited to correct typos.
Thank you for the replies so far. I appear to be unable to get the result I want, so I'd like to inquire on what I may have done wrong.
Code:
<After Eval>
var heal = user.atk;
var members = user.friendsUnit().aliveMembers().reduce((a, b) => a.hpRate() < b.hpRate()? a : b);
for (var i = 0; i < members.length; i++) {
var ally = members[i];
if (ally.isDead()) continue;
ally.gainHp(heal);
ally.startDamagePopup();
ally.clearResult();
}
</After Eval>
Code:
<After Eval>
let lowest = 1;
lowest = user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate());
let targets = user.friendsUnit().aliveMembers().filter(actor => actor.hpRate() == lowest);
targets[Math.randomInt(targets.length)].gainHp(user.atk);
</After Eval>
Code:
<After Eval>
var heal = user.atk;
var members = user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate());
for (var i = 0; i < members.length; i++) {
var ally = members[i];
if (ally.isDead()) continue;
ally.gainHp(heal);
ally.startDamagePopup();
ally.clearResult();
}
</After Eval>
Code:
<Post-Damage Eval>
let lowest = 1;
lowest = user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate());
let targets = user.friendsUnit().aliveMembers().filter(actor => actor.hpRate() == lowest);
targets[Math.randomInt(targets.length)].gainHp(25);
</Post-Damage Eval>
Posted above are the codes I tried, but neither of them has worked.
They did say its an additional effect, so the skill presumably does something else entirely, but also automatically heals the lowest hp% ally. it (afaik) doesnt have to target that ally.
This is correct. I recall some gacha games where the basic attack of the "Support" types of characters have a universal effect of restoring HP to an ally with the lowest HP, and that is what I wanted to emulate.
I'd like to inquire on what I may have done wrong.
ill say what went wrong with each from what i can see

<After Eval> var heal = user.atk; var members = user.friendsUnit().aliveMembers().reduce((a, b) => a.hpRate() < b.hpRate()? a : b); for (var i = 0; i < members.length; i++) { var ally = members; if (ally.isDead()) continue; ally.gainHp(heal); ally.startDamagePopup(); ally.clearResult(); } </After Eval>
array.reduce() returns a single value, not an array. by writing it into members and trying to do array operations on it, itll produce errors. you just had to use the gainHp(), startDamagePopup(), etc. on members directly, as it would already be the allied battler with the lowest hp%

<After Eval> let lowest = 1; lowest = user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate()); let targets = user.friendsUnit().aliveMembers().filter(actor => actor.hpRate() == lowest); targets[Math.randomInt(targets.length)].gainHp(user.atk); </After Eval>
this one was given to you, but theres 2 mistakes here. theres a missing ")" for the forEach, and since forEach doesnt return a value, lowest gets rewritten to be undefined after the forEach sets it to the party's lowest hpRate. that makes the filter() return an empty array, and you get an error for trying to use gainHp() on nothing

<After Eval> var heal = user.atk; var members = user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate()); for (var i = 0; i < members.length; i++) { var ally = members; if (ally.isDead()) continue; ally.gainHp(heal); ally.startDamagePopup(); ally.clearResult(); } </After Eval>
same issues as above with the missing ")" and members being undefined because forEach() doesnt return anything, but theres another one here. I assume that you meant to declare and initialize lowest beforehand, but thats missing. because of that, the code tries to initialize lowest within the forEach, but its definition includes itself while it already isnt initialized. thatll cause an error before youd get the one for members being undefined and trying to get its length

<Post-Damage Eval> let lowest = 1; lowest = user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate()); let targets = user.friendsUnit().aliveMembers().filter(actor => actor.hpRate() == lowest); targets[Math.randomInt(targets.length)].gainHp(25); </Post-Damage Eval>
I assume this is the latest one? its the same issues as the 2nd one, but should work after those are fixed. just going to make a slight adjustment so that you can call a popup on the same random target that was healed since you tried to do it on the one before this one

Code:
<Post-Damage Eval>
let lowest = 1;
user.friendsUnit().aliveMembers().forEach(actor => lowest = Math.min(lowest, actor.hpRate()));
let targets = user.friendsUnit().aliveMembers().filter(actor => actor.hpRate() == lowest);
targets = targets[Math.randomInt(targets.length)];
targets.gainHp(25);
targets.startDamagePopup();
</Post-Damage Eval>
/*EDIT:DIdnt notice the change in tags in your last one. good call.
Post-Damage is what you want for this since After Eval still gets processed on misses**/

that should work

if you press f8 during tests itll open up the console. errors will pop up there and should give you an idea of whats failing, where, and why
that should work

if you press f8 during tests itll open up the console. errors will pop up there and should give you an idea of whats failing, where, and why
Thanks! I've only tried the code you gave me, but I got it to work as intended now. Will try the rest later. 🙂
this one was given to you, but theres 2 mistakes here. theres a missing ")" for the forEach
My bad 😛

and since forEach doesnt return a value, lowest gets rewritten to be undefined after the forEach sets it to the party's lowest hpRate. that makes the filter() return an empty string, and you get an error for trying to use gainHp() on nothing
You're right, I dunno why I typed the lowest = at the beginning of that line. That's what I get for looking at, like, three sets of code at once ;_;

@MarxMayhem Just to add to what Robro said, there are a number of places where you're performing operations on the aliveMembers() array, and then you have a conditional checking for ally.isDead()

If your previous code is correct, that should be pointless, nothing above should allow you to be looking at a potentially dead actor.

Original thread: https://forums.rpgmakerweb.com/threads/skill-that-heals-ally-with-least-hp-percentage.164476/