MZ [Sloved]Help w/ skill common event, find Enemies w/ VS Passive State then add 1 new state to 1 random enemy in that specific group

Hello! 🙂 I have this skill that sometimes work but sometimes it doesn't and gives a script error in the console log, so I would like to ask for some help with it.

I am using Visustella's Passive States as part of this process. All plugins are latest version and arranged as on the website.

Effect: Below is the code I wrote for the skill as a Common event Extra effect unrelated to the target the player selected.
It is supposed to Find enemies with the Passive State 4, and then find 1 Random enemy from that group that isn't already effected by State 12 and apply State 12 to them. If all Passive State 4 enemies are effected by State 12, nothing happens.

Code:
//Create Array of Enemies with Visustella Passive State 4
const TotalLength = $gameTroop.aliveMembers().length;
let CanHit = [];

for (let i = 0; i < TotalLength + 1; i++){
  if ($gameTroop.members()[i].states().includes($dataStates[4])){
    CanHit.push($gameTroop.members()[i].index());
    };
};

console.log(CanHit);

//Create New Array of Enemies from CanHit that does not have State 12 applied
let TargetEnemyGroup = [];

for (let i = CanHit[0]; i < TotalLength + 1; i++){
if ($gameTroop.members()[i].isStateAffected(12) == false){
    TargetEnemyGroup.push(i);
    };
};

console.log(TargetEnemyGroup);

//Pick a Random Enemy from TargetEnemyGroup, that should be a target that has Passive State 4 and No State 12
//Add State 12 to the Random Enemy
if (TargetEnemyGroup.length > 0){
    const randomNum = Math.randomInt(TargetEnemyGroup.length);
    const EnemyIndex = TargetEnemyGroup[randomNum];
    $gameTroop.members()[EnemyIndex].addState(12);
    } else {
    console.log('All the free space is taken');
};

When it works, my console logs the arrays, so I know its working as intended.
But when it doesn't it shows me this error

1674914186598.png


I have a feeling that I made an error with the Passive State but I'm not exactly sure where I went wrong, so if someone could teach me if I have an error somewhere! Thank you.
Undefined means it does not exist. I'm not a very good coder, but I can see you have i < totalLength + 1. So what I believe is happening is you've gone outside of your array. Weird as it sounds, you actually want i to be less than your length at all times. Unless this is something in JavaScript I'm not familiar with.

Edit: Out of curiosity, can you print the value of
Code:
$gameTroop.members()[i] ?

Editing again because I didn't realize this was a thing.
@HexMozart88 Thank you very much for the correction! I added the + 1 to the length at some point because the effect kept missing out the last member. But you're right, the line starts at 0, so even if they are 7 enemies on screen it should stop looping at 6. My arrays print correctly again now!

Also since you asked, here you go. Since the 'i' is a number it would return with the actual Enemy at the index with $gameTroop.members()

I just figured that if the 'i' in the loop is a number, why not just use it as a variable for the enemy index as well, since the original script needs a enemy to check eitherway $gameTroop.members()[a].isStateAffected(b)

1674916076904.png

Original thread: https://forums.rpgmakerweb.com/threads/sloved-help-w-skill-common-event-find-enemies-w-vs-passive-state-then-add-1-new-state-to-1-random-enemy-in-that-specific-group.154470/