Enemy not attacking

So I am trying to make a ‘Enemy AI’ script. What it is supposed to do is that when the enemy ‘hits’ a player, the player takes damage. However, when I test it, the player doesn’t take damage. I was wondering if you guys can help me.

Here’s the ‘debounce’ script:

if (torso.Position - EnemyTorso.Position).magnitude < attackDistance and not ((torso.Position - EnemyTorso.Position).magnitude >= closeDistance) then
			nearValue = true
			EnemyHumanoid.WalkSpeed = 0
			if not debounce then
				debounce = true
				AttackAnim:Play() --Animation
				CanDamage = true --Allows the enemy to attack
				wait(Cooldown)
				CanDamage = false
				debounce = false
				if EnemyHumanoid.Health <= 0 then
					return
				end
			end
			if script.Parent:FindFirstChild("Hit")then
				return
			end
		else
			nearValue = false
			EnemyHumanoid.WalkSpeed = 16
		end

And here is the magnitude:

for i,v in pairs(BodyParts)do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid")and CanDamage == true then
			local Char = hit.Parent
			local Humanoid = Char:FindFirstChild("Humanoid")
			print("Humanoid Detected!")
		end
	end)
end

Any mistakes? Let me know! :smiley:

1 Like

I don’t see where the damage is given i.e. when whatever weapon hits it gives damage (i see hit perhaps thats where it should be?)
assuming if script.Parent:FindFirstChild(“Hit”) means is the player that gets hit
Hit.Parent.Humanoid:TakeDamage(20)

Oh whoops! My bad! I will show you the details of the magnitude:

for i,v in pairs(BodyParts)do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid")and CanDamage == true then --If the enemy's hand detects a humanoid and if the CanDamage value is equal to true
			local Char = hit.Parent --the character of the detected humanoid 
			local Humanoid = Char:FindFirstChild("Humanoid") --gets the humanoid
			print("Humanoid Detected!")
		end
	end)
end

ok, wheres the code to actually remove hp from player though? I just see the hit being detected

You have the humanoid therein, so you should have code to remove hp

Something like Humanoid:TakeDamage(20) because you detect if hit is true and candamage is true, remove hp, 20 is whatever value you want

The print function is used for checking if it works. It used to be the place where the ‘TakeDamage’ was but before then, it didn’t work.

Oh so you’re saying the function doesnt work at all? Also I am not referring to your print though it is good for checking if it works like you said

if hit.Parent:FindFirstChild("Humanoid")and CanDamage == true then --If the enemy's hand detects a humanoid and if the CanDamage value is equal to true
		local Char = hit.Parent --the character of the detected humanoid 
		local Humanoid = Char:FindFirstChild("Humanoid") --you have the target humanoid
		Humanoid:TakeDamage(20) --since hit and candamage true, remove hp from target
		print("Humanoid Detected!")
	end

I hope you found a solution to this.

Ok so I found the solution to the problem. All I had to do was place the magnitude into the debounce segment. It was really stupid of me but in all words, thank you for the appreciation.