How would I use debounce to wait for my animation to stop and then I can activate again?

Hello Developers!

I was messing around with my tool when I came across a problem where it would play the animation and attack if I clicked. I thought about using debounce to stop the player from spamming an attack but I just don’t know how I would do this with this specific part of my code.

script.Parent.Activated:Connect(function()
		if canAttack == true and activated == true then
			attack:Play()
			script.Parent.Stun.Swing:Play()
			script.Parent.Hitbox.Touched:Connect(function(hit)
				if not debounce then
					debounce = true
						if hit.Parent:FindFirstChild("Humanoid") ~= nil then
						hit.Parent.Humanoid:TakeDamage(2)
						script.Parent.Stun.StunHit:Play()
					end
				end
			end)
			attack.Stopped:Wait()
			attack:Stop()
		end
end)

Thank you,

Ultan

script.Parent.Activated:Connect(function()
		if canAttack == true and activated == true then
			canAttack = false
			attack:Play()
			script.Parent.Stun.Swing:Play()
			local conn
			conn = script.Parent.Hitbox.Touched:Connect(function(hit)
				local hum = hit.Parent:FindFirstChild("Humanoid")	
				if hum ~= nil then
					hum:TakeDamage(2)
					script.Parent.Stun.StunHit:Play()
					conn:Disconnect()
				end
			end)
			attack.Stopped:Wait()
			conn:Disconnect()
			canAttack = true
		end
end)
1 Like