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)
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)
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.
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.
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.
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)