Calling Enemy Index as Variable/Control Enemies

So I'm on the verge of making a semi-okay way to replicate monster controlling (having a character charm a monster and control their exact actions). First I apply the charmed state. If successful, user gains skillset of that monster. I store the monster's index for later use with this note tag in the charmed state notebox:


<Custom Apply Effect>


var stateindex = target.index();


</Custom Apply Effect>


Then I use the following code in a skill to force that monster to use their skill on a target of my choice:


<After Eval>


BattleManager.queueForceAction($gameTroop.members()[stateindex], 284, -2);


</After Eval>


The error I get is that the stateindex is "never defined". This one's making me scratch my head.

Original thread: https://forums.rpgmakerweb.com/threads/calling-enemy-index-as-variable-control-enemies.71812/