Making a Trap Skill

Ok, hi. This is my first time posting, and I've only had RPG Maker MV for a few weeks. So, sorry in advance if this is the wrong section/this has been answered before.

Anyway, I've been trying to make a skill where a character can put a "trap" on another character. If an enemy attempts to attack this character, they will automatically miss, and be unable to move for a few turns.

At the moment, I'm trying to do it like this:
Using the skill puts a state on the ally you use it on. If a person with this state is attacked, the enemy doing the attacking is given the "trapped" state. However, I can't figure out how to make the game sense when the trap is "triggered", and by who.

Any help would be greatly appreciated.
This is as far as i can go, at least you will apply a state on the enemy when the target is attacked.
Just remember that X is the ID of the state

<Custom React Effect>

if (this.isHpEffect() && value > 0) {

user.addState(x);

if (user.isDead()) {

user.performCollapse();

}

}

</Custom React Effect>
What @kovak posted should work. But more importantly @Unoriginal_Username, you need to have Yanfly plug ins for those note tags to work. (Yanfly Buff &State core more specifically). If you need more help, just reply, the trap skill you are mentioning is pretty simple to accomplish with those plug ins.
What @kovak posted should work. But more importantly @Unoriginal_Username, you need to have Yanfly plug ins for those note tags to work. (Yanfly Buff &State core more specifically). If you need more help, just reply, the trap skill you are mentioning is pretty simple to accomplish with those plug ins.
Thanks! I have the plug in installed, but have no experience with the coding. Where exactly do I need to put it?

EDIT: Never mind, figured it out after a bit of trial & error. Thanks for the tip about the plugin.
This is as far as i can go, at least you will apply a state on the enemy when the target is attacked.
Just remember that X is the ID of the state
Thank you! This works perfectly!

Original thread: https://forums.rpgmakerweb.com/threads/making-a-trap-skill.100413/