Help with "protecting" mechanic in "mon" game

I'm making a "mon" game (think Pokemon, Digimon) in RMMZ where one of the major gimmicks is that the "trainers" can fight alongside their monsters. Another one of the game's mechanics is that if a trainer on either side goes down, they won't be able to command their monsters and will automatically lose the battle. To prevent players from going "straight for the commander," the first monster in a trainer's party has a "protecting" role where they protect their trainer from harm. I haven't been able to successfully pull it off, however.
I've tried the Substitute flag, but it doesn't seem to work for enemies, which is obviously a problem. I've also tried a "protected" state that reduces all damage done to a trainer to zero, but it doesn't feel realistic (when I attack an enemy trainer it reduces damage to zero, but the monster protecting the trainer doesn't take any damage either, which makes no sense) and while I've had success in doing this for enemies (I just make a battle event so that when the "protecting" monster goes down it removes the protected state on the trainer and allows them to be harmed), I'm struggling to make it work for party members. Is there any way I could make a "protecting" mechanic that works the same way for both party members and enemies?
Moved to: MZ Support

Game Mechanics Design is for discussing and getting feedback on mechanics. The support forums are where you go to get help actually implementing a mechanic.
Are you using the VisuStella plugins? You can accomplish this pretty easily with a passive state with a <JS Pre-Start Action> which checks whether the trainer is being targeted and the monster is alive, and if so sets the target of the action to the monster instead.
Are you using the VisuStella plugins? You can accomplish this pretty easily with a passive state with a <JS Pre-Start Action> which checks whether the trainer is being targeted and the monster is alive, and if so sets the target of the action to the monster instead.
I'm not very experienced with JavaScript. How would I pull this off?
More specifically, how do I make it so that this targets an actor/enemy in a certain index rather than a specific actor/enemy?
Are you using the VisuStella plugins? You can accomplish this pretty easily with a passive state with a <JS Pre-Start Action> which checks whether the trainer is being targeted and the monster is alive, and if so sets the target of the action to the monster instead.
I tried inputting some JavaScript code but it doesn't work at all.
if($gameParty.members()[2].isAlive()){
Game_Action.prototype.setTarget($gameParty.members()[2]);
}
It runs, but enemies can still target other party members. I also haven't figured out how to check if an enemy is targeting the trainer.
Yeah I'll admit I have no idea what I'm doing

Original thread: https://forums.rpgmakerweb.com/threads/help-with-protecting-mechanic-in-mon-game.134800/