[Need Help] a way to detect when a NPC is attacking you

This feels kind of like a dumb question, but I did google it and didn’t find really what I was looking for.

But basically, I’m wondering if there is any way to detect when a player is attacked by an npc in an if statement especially (assuming it’s able to be a function).

I haven’t tried any code, mainly because I couldn’t think of an alternative way to detect, and like I said no code was code found when I searched for it. If you are possibly doing any code, if you could also so kindly as to explain a bit so that I may learn from it.

Have you tried http://wiki.garrysmod.com/page/NPC/GetEnemy