Attack keeps damaging player even after cooldown!

Hello!

So I am working on a transfur game and I was implementing a transfur attack system until I came across a problem.

When the tool is activated, it’s supposed to damage a human by 25 hp however it will do it more than once!

Script
script.Parent.Activated:Connect(function()
	if script.Parent.Cooldown.Value ~= true then
		local Animation = script.Parent.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.AttackAnimation)
		Animation:Play()
		
		script.Parent.Cooldown.Value = true
		script.Parent.Parent["Left Arm"].Touched:Connect(function(Hit)
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if Humanoid then
				Humanoid:TakeDamage(25)
			end
		end)
		else
	end
	wait(3)
	script.Parent.Cooldown.Value = false
end)
Video

Thanks! Waiting for a reply.

Have you tried to use Length to wait until the animation is done?

You could use that in your code and it might help.
If that didn’t help -

You should add a debounce in your touched event.

1 Like

That seemed to work and it no longer damages the player even after cooldown but after debounce is set to false it damages the player yet again.
Do you think there’s a way I can solve this issue?

EDIT: Found a way to (kinda) solve it and by kinda I mean it because even after the animation is done if you approach a player it’ll still damage the player until the tool is activated again.

Ok, so I think the issue is likely this part of your script:

script.Parent.Parent["Left Arm"].Touched:Connect(function(Hit)
	local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
		if Humanoid then
		Humanoid:TakeDamage(25)
	end
end)

Primarily the fact that it is checking for every time the part is touched. So no matter what happens, regardless of the cooldown, it will trigger because the left arm is touched. Instead I would advise you :Disconnect() your function once it’s been used once - this will prevent it firing again. I adapted your script - see if this works.

local connection

script.Parent.Activated:Connect(function()
	if script.Parent.Cooldown.Value ~= true then
		script.Parent.Cooldown.Value = true
		local Animation = script.Parent.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.AttackAnimation)
		Animation:Play()
		
		connection = script.Parent.Parent["Left Arm"].Touched:Connect(function(Hit)
			connection:Disconnect()
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if Humanoid then
				Humanoid:TakeDamage(25)
			end
		end)
	else
	end
	wait(3)
	script.Parent.Cooldown.Value = false
end)

Wowie! Thanks so much!

Tried using disconnect() to but not this way

Thanks so much!
Have a nice day! (if it’s even day anymore)