Need help with afflicting a state after being attacked onto the attacker

I'm creating mechanics for each of my characters (RMMZ), and having a heck of a time trying to figure out how to do one of them. I basically want it so that when an enemy attacks the specific character, there is a chance that the attacker is afflicted with a state debuff. I've been trying for a couple hours now playing around with common events and such to try and make it work but I can't seem to find a way to:
1. Make the state apply when the specific character is attacked
2. Have that state apply specifically to the attacker
Is it possible to get something like this to work? Either with the program's tools or if there is an existing plugin that would allow this to happen? I'd very much appreciate any help thrown this way.
Hello, with VisuStella's Battlecore plugin I was able to create this mechanic using the
<JS Post-Damage as Target> notetag on state, here is the code I used:
JavaScript:
<JS Post-Damage as Target>
if (user.isStateAffected(state_number) == false)
{
   $gameVariables.setValue(var#, Math.randomInt(100) + 1);
   if ($gameVariables.value(var#) >= 50)
   {
     user.addState(state_number);
   }
};
</JS Post-Damage as Target>
Where state_number is to be replaced with the state afflicted onto the attacker, and var# is a controlled variable dedicated to randomly generated numbers...whatever variable number you want really.

This will give the attacker a 50% chance to be inflicted with a specific state when landing a successful hit to a user that is affected with the state using this notetag AND they are not already inflicted.

If you should desire for the state to have a chance to be rerolled every hit, simply remove the first conditional branch.

Additionally, you could stick this script call before or after "user.addState(state_number);"

$gameTemp.requestAnimation([user], AnimationID);

to add an animation to the attacker as well.
Hello, with VisuStella's Battlecore plugin I was able to create this mechanic using the
<JS Post-Damage as Target> notetag on state, here is the code I used:
JavaScript:
<JS Post-Damage as Target>
if (user.isStateAffected(state_number) == false)
{
   $gameVariables.setValue(var#, Math.randomInt(100) + 1);
   if ($gameVariables.value(var#) >= 50)
   {
     user.addState(state_number);
   }
};
</JS Post-Damage as Target>
Where state_number is to be replaced with the state afflicted onto the attacker, and var# is a controlled variable dedicated to randomly generated numbers...whatever variable number you want really.

This will give the attacker a 50% chance to be inflicted with a specific state when landing a successful hit to a user that is affected with the state using this notetag AND they are not already inflicted.

If you should desire for the state to have a chance to be rerolled every hit, simply remove the first conditional branch.

Additionally, you could stick this script call before or after "user.addState(state_number);"

$gameTemp.requestAnimation([user], AnimationID);

to add an animation to the attacker as well.
Oh my goodness! Thank you so much, this works EXACTLY how I needed it to! I really appreciate your help with this, and thank you also for even adding in the extra bit for adding an animation to it. I had wondered if there was something in VisuStella that could do this but I wasn't sure exactly where to look for it!
This is a good opportunity to review MZ's new variable controls. MZ keeps track of the last Actor/Enemy to act, to be targeted, as well as the last skill id they used. This data can be assigned to variables and used for conditional branches.

What you're wanting to do might be accomplished like this.
-Have the Enemy use a Unique Attack that turns on a Switch (via a common event)
-In the Troop Battle Event create a page that uses this Switch as a condition, with the Span set as "Moment"; so that this event will run right after the Enemy attacks.
-Setting Variables using the corresponding "last" game data, you can create a conditional branch that checks if the last targeted actor was -whoever-
-if true, nested under this is a number of conditional branches depending on the number of enemies in the troop. So if the last Enemy to attack was #1, add your desired status effect to enemy #1.
- for the last step, turn the switch off at the end so the battle will continue.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-afflicting-a-state-after-being-attacked-onto-the-attacker.139505/