I’ve made a tool that has animations, but I don’t know how to add a cooldown so here is what I did
local player = game:GetService("Players").LocalPlayer
local cooldown = 5
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid.Animator
local Hammer = script.Parent
Hammer.Activated:Connect(function()
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://121844901937516"
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
local play = true
if play == true then
wait(cooldown)
Hammer.Unequipped:Connect(function()
animationTrack:Stop()
end)
end
end)
please help if you have any idea on what to do
edit: I want a cooldown for the animation
local debounce = true
Hammer.Activated:Connect(function()
if not debounce then return end
-- ...
debounce = false
task.wait(cooldown)
debounce = true
end)
You need to add something called debounce.
Debounce is basically a boolean variable which if true, allows you to attack; if false, does not let you attack.
EDIT: Kinda late, the guy (two posts) above has an example.
EDIT 2: If you want cooldown to be based on animation length, just set it to task.wait(Track.Length)
Add the if not debounce check before everything else in your function, and everything after the -- ... to the end of the function.
This prevents the hammer from being used again until the cooldown ends. If you want only the animation to have a cooldown, move the debounce check from the start of the function and wrap the part that creates the animation in it.
local player = game:GetService("Players").LocalPlayer
local cooldown = 5
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid.Animator
local Hammer = script.Parent
Hammer.Activated:Connect(function()
local debounce = true
Hammer.Activated:Connect(function()
if not debounce then return end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://121844901937516"
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
local play = true
if play == true then
wait(cooldown)
Hammer.Unequipped:Connect(function()
animationTrack:Stop()
debounce = false
task.wait(cooldown)
debounce = true
end)
end
end)
end)
local player = game:GetService("Players").LocalPlayer
local cooldown = 5
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid.Animator
local Hammer = script.Parent
local debounce = true
Hammer.Activated:Connect(function()
if not debounce then return end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://121844901937516"
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
debounce = false
task.wait(cooldown)
debounce = true
end)
local player = game:GetService("Players").LocalPlayer
local cooldown = 5
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid.Animator
local Hammer = script.Parent
Hammer.Activated:Connect(function()
local debounce = true
if not debounce then return end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://121844901937516"
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
local play = true
if play == true then
task.wait(cooldown)
Hammer.Unequipped:Connect(function()
animationTrack:Stop()
debounce = false
task.wait(cooldown)
debounce = true
end)
end
end)