MV How to make all enemies attack an enemy affected by a state?

(Note: As suggested by @ATT_Turan, I created a post to ask for help with this specific issue.)

So, I'm using some of Yanfly's plugins (namely Buffs & States Core) to create a state that should make all enemies turn against the enemy affected by that state. It should work like this:

1) One actor applies state #55 onto one enemy to mark it as the "target."

2) After that, all the other enemies receive state #116 to make them turn against the "target."

3) During that turn, all enemies with state #116 will attack the enemy with state #55.

So far, step 3 is the only one that isn't working. Here's the code used for state #116:

Code:
<Custom Action Start Effect>
var action = user.currentAction();
var foes = $gameTroop.aliveMembers();
for (var i = 0; i < foes.length; i++) {
  var foe = foes[i];
  if (foe.isStateAffected(55)) {
    action.setTarget(foe.index());
  }
}
</Custom Action Start Effect>

@Robro33 said it worked for them, but it seems they applied one of the states onto actors instead of enemies. My intention is to make all enemies affected by state #116 attack the enemy affected by #55, kinda like "confusion/chaos" but all attacks are directed toward the same target.
My intention is to make all enemies affected by state #116 attack the enemy affected by #55, kinda like "confusion/chaos" but all attacks are directed toward the same target.
right, forgot you mentioned that part of it the other day. the issue with this is that when you set the target like that, the scope is still respected. It wont make a skill that targets enemies change to target allies.
its why i had suggested back then to make extra damaging ally scope skills with a selection restriction and make enemies prioritize using them with the action patterns.
As @Robro33 pointed out, the only thing that setTarget() does is change the index of who's being targeted - you can see that, because the only argument you're providing is the index.

It doesn't change which of the units (troop/party) that index is coming from.
@Robro33 @ATT_Turan ok, so I changed state #116's restriction to "attack an ally" to change the scope... but now the enemies are attacking each other instead of the one affected by state #55.
if its unclear, i mentioned the Selection Control plugin in the other thread because the approach i described needed it.

you arent changing the restriction of the state. youre making a duplicate skill for what the enemies should be using on eachother, making its scope be to target allies, and youre giving that skill a selection restriction through that plugin so that it can only be used on a target with state 55. then you make enemies prioritize using it with action patterns by making them 9 and not having other skills above 6

Without doing that or the thing i mentioned with the ai core in the other thread, you would need plugin that changes how targets are made. What you want is possible with yanflys target core plugin, but you would have to make every skill contain a note tag to target an ally with state 55 if the user has state 116
Setting a restriction doesn't change the scope of the skill. That restriction overwrites the entire normal targeting process.

The code for Action Start Effect runs before the target is chosen.
wouldn't it work better if they apply a state and the once that doesn't have it
attack the target instead?
this saves 1 state though, you just check if 1 has state 55, if there is,
the troop attack it, that is !affect by state 55 and remove after that.
Problem solved! Here's the solution step-by-step:

State description: marks one enemy as the "target" and makes all the other enemies attack that target until it's defeated (like the "confusion" state, except all attacks are directed toward the same target).

Requirements: Yanfly's Buffs & State Core plugin + Yanfly's Taunt plugin.

1) Create a state ("Target") to set the selected enemy as the target .

2) Create another state ("Attacker") to make all the other enemies attack the "Target", then set the state's "restriction" to "attack an ally."

3) Add the following code to the Target state's notetag, to make the enemy effectively become the target of all attacks:

Code:
<Physical Taunt>
<Magical Taunt>
<Certain Taunt>

4) Also add the following code to the Target state's notetag, which applies the "Attacker" state onto all the other enemies whenever an enemy receives the "Target" state (be sure to replace "target_state_id" and "attacker_state_id" with the appropriate state IDs for each):

Code:
<Custom Apply Effect>
var foes =  $gameTroop.aliveMembers();
for (var i = 0; i < foes.length; i++) {
  var foe = foes[i];
  if (!foe.isStateAffected(target_state_id)) {
    foe.addState(attacker_state_id);
  }
}
</Custom Apply Effect>

5) Finally add the following code to the Target state's notetag as well, which removes the "Attacker" state from the other enemies when the "Target" is defeated (or else, the enemies will start attacking each other randomly; again, be sure to replace the state IDs):

Code:
<Custom Respond Effect>
if (target.hp <= 0) {
  var foes =  $gameTroop.aliveMembers();
  for (var i = 0; i < foes.length; i++) {
    var foe = foes[i];
    if (foe.isStateAffected(attacker_state_id)) {
      foe.removeState(attacker_state_id);
    }
  }
}
</Custom Respond Effect>

Just keep in mind that this isn't exactly a tutorial, since it was designed to solve my specific issue... but hopefully it might also help someone with a similar problem in the future.

Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-all-enemies-attack-an-enemy-affected-by-a-state.173079/