Hack and Slash Mechanics (attacking and not taking damage)

I’m making a hack-and-slash roguelike game. When you attack the enemies(when you are in collision with them and you press the space bar to attack), they take damage. And when they hit you(come in collision with you), you take damage. When I attack them, however, I deal damage and take damage. How do I make it so you can attack them without taking damage at the same time?

Any help would be greatly appreciated.

try using a Boolean variable to indicate whether the player is attacking, then use it as a condition for the player taking damage. this should fix the issue


thanks a lot! that really helped out.