MV Assign Member Party Level to Variable

I have 4 variables to assign different level for actor recruitment. I would like to ask help how to set this up using the script call even function. thank you

1. New actor level is equal to average party level
2. New actor level is equal to level of member with highest level
3. New actor level is equal to level of member with lowest level
4. New actor level is equal to level of Main character (database ID actor 17), even if not in active party
var avgLevel = 0;
var lowestLevel = 999;
var highestLevel = 0;
var specificLevel = 0;
for(var i = 0; i < $gameParty.members().length; i++) {
avgLevel += $gameParty.members().level;
if($gameParty.members().level > highestLevel) {
highestLevel = $gameParty.members().level;
}

if($gameParty.members().level < lowestLevel) {

lowestLevel = $gameParty.members().level;

}
if($gameParty.members().actorId() == 17) {
specificLevel = $gameParty.members().level;
break;
}


}
console.log(avgLevel / $gameParty.members().length + " / " + highestLevel + " / " + lowestLevel + " / " + specificLevel)
@boomy you seem to have missed your[i] while iterating through the members() array.
JavaScript:
// For current party only
// -> To use all members replace "battleMembers()" with "members()"
const levels = $gameParty.battleMembers().map(e => e.level);

// Average Level
let newLevel = levels.reduce((a,b) => a + b, 0) / levels.length;

// Lowest Level
let newLevel = Math.min(...levels);

// Highest Level
let newLevel = Math.max(...levels);

// Party leader
let newLevel = $gameParty.leader().level;

// Specific actor (the actual leader, as you mentioned in OP):
let newLevel = $gameActors.actor(17).level;

EDIT3 (last one I swear): Alternatively if you want all the variables in one script call, just change their names so they're unique (or call the $gameVariables.setValue(id, newLevel) after each one).

EDIT2: Actually forgot 1 more detail. After you've assigned the levels and determine which newLevel to use, then of course add the following to assign the variable:
JavaScript:
// Replace "variableId" with the ID of the variable that stores the value.
$gameVariables.setValue(variableId, newLevel);

EDIT: Okay there... I think I'm done editing my mistakes lol. Hope this helps.
It was added, but because it wasn't put into code tags, it was interpreted as formatting code, and everything after is showing in italics.
I tried the script command.. here are the results.. its not correct. it returns the value for the MC level.

MC = 21
Actor 2 = 9
Actor 3 = 3
Actor 4 = 16

5.25 / 21 / 21 / 21

JavaScript:
var avgLevel = 0; var lowestLevel = 999; var highestLevel = 0; var specificLevel = 0;
for(var i = 0; i < $gameParty.members().length; i++) {
avgLevel += $gameParty.members()[i].level;
if($gameParty.members()[i].level > highestLevel) { highestLevel = $gameParty.members()[i].level;}
if($gameParty.members()[i].level < lowestLevel) {lowestLevel = $gameParty.members()[i].level;}
if($gameParty.members()[i].actorId() == 17) {specificLevel = $gameParty.members()[i].level;break;}
}
console.log(avgLevel / $gameParty.members().length + " / " + highestLevel + " / " + lowestLevel + " / " + specificLevel)
$gameVariables.setValue(6,avgLevel);$gameVariables.setValue(7,highestLevel);$gameVariables.setValue(8,lowestLevel);$gameVariables.setValue(9,specificLevel);
$gameVariables.setValue(10,Math.floor(Math.random() * (highestLevel-lowestLevel)) + avgLevel);

EDIT: @lordvalinar thanks your code works
EDIT2:- Actually $gameParty.highestLevel() gets all members, not just current party.. so good to know for future 🙂
EDIT: Glad it could help 🙂

I stand corrected... (on the "last edit").. turns out Game_Party() has a function already that retrieves the highest level.

JavaScript:
// The script call
let newLevel = $gameParty.highestLevel();

// Function (basically same thing as I posted already) - rmmz_objects.js line 5529
Game_Party.prototype.highestLevel = function() {
    return Math.max(...this.members().map(actor => actor.level));
};

Or since this is actually RMMV - rpg_objects.js line 4913, but yeah same thing. All members.
Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/assign-member-party-level-to-variable.142545/