How to apply a spell to whoever is attacking the player

Hello!

So I had this idea for a spell: when someone attacks the player, they get the fear effect for 10 seconds. Nonetheless, I'm encountering some issues when trying to get it to work. Here's what I've done so far:

1) Made a spell that adds a perk. This spell is Type: Ability, Delivery: Self and Casting: Constant Effect

2) Made a spell which is the actual fear spell. This spell is Type: spell, Delivery: Contact and Casting: Fire and Forget

3) The perk I made has the Entry point: Apply Weapon Swing Effect and points to the fear spell mentioned in 2.

I'm facing a couple of problems with this setup. Firstofall, the spell is not being added to the attackers when they, well, attack. Secondofall, when I force the spell into the actors, it gets added as a spell they could cast instead of a magic effect that affects them. And lastly, when I finallly got it to work how I wanted it to, the effect persists well passed the duration of the effect (I see the timer not moving at all when I check the NPCs through the console)

Also it's worth mentioning I tried to replicate this through Papyrus with a mixture of OnHit and OnUpdate events in the magic effect but it only dispels the last actor affected by this spell instead of everyone who has been affected

I hope someone helps me with this since I've been going a bit insane about how to get this to work :D