How do I add debounce to this?

Hello everyone for the 1 millionth time.

I am having this issue were I can’t add a debounce to equipping a tool. This is the code already I just don’t know what to put in?

Tool.Equipped:Connect(function()
	ToolEquipAnimation:Play() -- Plays animation when equipped
		
	ToolEquipAnimation.Stopped:Connect(function()
		ToolIdleAnimation:Play() -- When equip animation stops, it plays the tool idle animation which is a looping animation
	end)
end)

Tool.Unequipped:Connect(function() -- When tool is unequipped, it gets all the playing animations and stops them
	for _, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		if AnimationTrack.Name == "R15ToolIdle" or AnimationTrack.Name == "R6ToolIdle" then
			AnimationTrack:Stop()
		end
	end
end)
2 Likes

Wow must be very hard to crack…

1 Like

What do you want to have the debounce for? Could you please explain why you want a debounce and how you want it to work?

Here is an example of a debounce:

local DB = false -- debounce

Tool.Equipped:Connect(function()
	if not DB then -- if debounce is false
		DB = true -- makes debounce to true
		ToolEquipAnimation:Play() -- Plays animation when equipped
		
		ToolEquipAnimation.Stopped:Connect(function()
			ToolIdleAnimation:Play() -- When equip animation stops, it plays the tool idle animation which is a looping animation
		end)
		task.wait() -- time here
		DB = false
	end
end)
3 Likes

Hello, sorry for late reply.

I wanted to add a debounce system to stop the player from spam equipping the tool. I tried the code and put in a time for the task.wait() but it still is spammable.

What number value did you put in the task.wait()?

I put a 2 second delay before setting the debounce to false.

So after 2 seconds the player can re-equip it, or are they able to do it instantly over and over?

If the tool is equipped then make a boolvalue for it, for example let’s call it equipped. When equipped = true make it so that the function won’t run at all when the tool is already in use.

This might be what you’re looking for. How to add a equip cooldown for a tool?

Yes the player can instantly equip over and over.

I basically just want a cooldown for equipping the tool.

I think I wrote the code wrong:

ToolDebounce = false

Tool.Equipped:Connect(function()
	if ToolDebounce == false then
		ToolEquipAnimation:Play()
		
		ToolEquipAnimation.Stopped:Connect(function()
			ToolIdleAnimation:Play()
		end)
		
		ToolDebounce = true
	elseif ToolDebounce == true then
		
		task.wait(2)
		
		ToolDebounce = false
	end
end)

You can do something like this:

local plr = Tool.Parent

local DB = false


Tool.equipped:connect(function()

If DB then
plr.Character.Humanoid:UnequipTools()
else
—what you would do here
end
end)

I have put this but where do I set the debounce to false?

local ToolDebounce = false

Tool.Equipped:Connect(function()
	if not ToolDebounce then
		ToolDebounceEvent:FireServer()
	else
		ToolEquipAnimation:Play()
		
		ToolEquipAnimation.Stopped:Connect(function()
			ToolIdleAnimation:Play()
		end)
	end
end)

I don’t think I’m fully understanding. Do you want whatever happens when the tool is equipped to have a cool down, or do you want a generic cool down that doesn’t allow the player to hold to item until the cool down is complete?

I want a generic cooldown that doesn’t allow the player to hold the item until the cooldown is complete. Sorry that the :FireServer event might confuse you. It unequips the tool on the server as you can’t change it on the client because it displays an error when using :UnequipTools() on the client.

When i have nowhere to put a timer, i throw it in a coroutine.wrap function. It will keep running while you go about your business.

I’m not sure where you’d want the cool down to start. Could you specify that?

The debounce can’t stay false forever otherwise the debounce won’t work. It needs for example:

wait(2)

Debounce = false

But I am not sure on where to put this?

Oh okay. If you didn’t want them to spam the tool, you would probably put that in the unequipped function (so once they unequip it, they can’t equip it for 2 seconds

Do something like


local plr = Tool.Parent

local DB = false


Tool.equipped:connect(function()

If DB then
plr.Character.Humanoid:UnequipTools()
else
—what you would do here
end
end)

Tool.Unequipped:connect(function()
 DB = true

task.wait(2)

DB = false

end)

Sorry to say but yes I do have a unequip function and it has a for loop inside which stops all running animation where should I put it now?

Tool.Unequipped:Connect(function()
	for _, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		if AnimationTrack.Name == "R15ToolIdle" or AnimationTrack.Name == "R6ToolIdle" then
			AnimationTrack:Stop()
		end
	end
	
	ToolDebounce = true
	
	task.wait(2)
	
	ToolDebounce = false
end)

I don’t have my computer with me right now, but I’m pretty sure that it should work nonetheless?

If it doesn’t work try making another unequipped function to do it