Debounce not working

In this script, after you click to swing a sword, I want it so you are unable to click for another 5 seconds, using debounce. However I simply can’t get it working and you can spam the sword (essentially, debounce isn’t working, or I’ve located the lines of code incorrectly.

wait()
local debounce = false
local tool = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid
local anim = hum:LoadAnimation(script.Parent.KatanaEquipAnim)
local attackanim = hum:LoadAnimation(script.Parent.UpdatedKatanaSwingAnim)
local mouse = player:GetMouse()

anim.Priority = Enum.AnimationPriority.Action
attackanim.Priority = Enum.AnimationPriority.Action

script.Parent.Equipped:Connect(function()
	anim:Play()
end)

script.Parent.Unequipped:Connect(function()
	anim:Stop()
end)

if debounce then return end

script.Parent.Activated:Connect(function()
	debounce = true
		attackanim:Play()
		print("sword hit")
		tool.Handle.Swing:Play()
		
		wait(5)

		debounce = false
end)

debounce is a variable you’ve declared. Utilizing that variable to create a debounce is done by creating a conditional statement that checks if it is true or false. In your case you’d want it to be swung when false so you have to check if its false.

script.Parent.Activated:Connect(function()
        if debounce == false then
 	        debounce = true
		    attackanim:Play()
		    print("sword hit")
		    tool.Handle.Swing:Play()
		
		    wait(5)

		    debounce = false
        end
end)
5 Likes

I see what I did wrong now. It works perfectly now.

1 Like