I followed to tutorial to make knockback when hitting an enemy, only the first instance of the enemy gets knockback

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alex10_0

So I followed a tutorial for making a knockback system, the first one that pops up on youtube, fiddled around with it a bunch but made it to work, but when I copy paste an enemy and try to knockback the other enemy it just knockbacks the original instance. I genuinelly have no idea why this happens, there is a comment that has the same exact issue, here is the code:

func punch_knockback():
	if knockback == true and is_on_floor():
		if facingright != player_dir:
			facingright = player_dir
			scale.x = -scale.x
	if knockback == true:
		if facingright == true:
			motion.x = 500
		elif facingright == false:
			motion.x = -500
	if knockback == true:
		motion.y = -100
			if $Sprite/detectwallbounceright.is_colliding() and facingright == true and bouncetimer > 0.1:
			motion.x = -60
			facingright = false
			bouncetimer = 0
			scale.x = -1
			Global.camera.shake(0.1,0.5)
			frameFreeze(0.01, 0.2)
		elif $Sprite/detectwallbounceright.is_colliding() and facingright == false and bouncetimer > 0.1:
			motion.x = 60
			facingright = true
			bouncetimer = 0
			scale.x = -1
			Global.camera.shake(0.1,0.5)
			frameFreeze(0.01, 0.2)
		
		motion.x = clamp(motion.x,-knockmaxspeedx,knockmaxspeedx)
		if knocktimer > maxknocktime:
			knockback = false

here is the code from the player:

	for body in $attack.get_overlapping_bodies():
	if knockback_wait <= 0 and body.get("NAME") == "enemy" and punchslide == true:
		knockback_wait = 50
		emit_signal("knockback")
		frameFreeze(0.01, 1.0)
		Global.camera.shake(0.2,2)

Thank you all in advance

:bust_in_silhouette: Reply From: Hut

You should add the video URL and the comment so the community can help faster. Anyway, I watched the video and saw a problem, you need to pass the enemy body using the signal, then you are already fixing the bug of all enemies getting effected.
Also make sure to connect all the enemies, so you can get rid of the “only first instance is affected”.

Player script:

signal knockback(enemy_body)

...

for body in $attack.get_overlapping_bodies():
    if knockback_wait <= 0 and body.get("NAME") == "enemy" and punchslide == true:
        knockback_wait = 50
        emit_signal("knockback", body) #pass the body that is on Area2D

Enemy script:

func _on_player_knockback(body):
    if body != self: #cancel if body isn't this enemy
        return
    // rest of the code

The tutorial: youtube.com/watch?v=OOnI8LUDXz0