Hello! I am trying to create a animation playing on click script, which the one I composed is working, but I need to figure out how to make it unspammable (cooldown on playing of animation). I have tried wait() but am thinking I’m putting it in the wrong spot. this is the script I’m using
local anim = tool:WaitForChild("Animation")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local loadanim = humanoid:loadAnimation(anim)
tool.Activated:Connect(function()
loadanim:Play()
end)```
Any help would be appreciated!
local anim = tool:WaitForChild("Animation")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local loadanim = humanoid:loadAnimation(anim)
local deb = false
tool.Activated:Connect(function()
if deb == false then
deb = false
wait(1)
deb = true
loadanim:Play()
end
end)
deb = false
In other words,
A Debounce is a Block of Code you add into your script to wait until. A form of Delay if you will.
They usually consist of a:
Boolean
Determines the Activity of the Cooldown.
if Statement
Checks if the Cooldown is active.
Example of use:
DTime = 2 -- Delay Time
Debounce = false -- Determines whether the Event could fire or not
Tool.Activated:Connect(function()
if not Debounce then -- Checks if Debounce is able to be used
Debounce = true -- Since its now set to true and the Script is looking for it to be false, it wont fire until then
-- code when fired
task.wait(DTime) -- Delay
-- code when delay ends
Debounce = false -- Event ready to be used again
end
end)
The Two Posts Above mine are Correct in uses of a Debounce, but lack in detail about what they are.
Also, Another suggestion, use the Animator to load animations as the LoadAnimation on the Humanoid is Deprecated. (Discontinued, No longer in use / Updated, Planned to be removed)
(Sorry for all the editing, trying to add sufficient detail)