I'm making a hammer tool, and I added animations but there is no cooldown

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)

do you mean a cooldown before it plays?

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)

like it waits 1 second before you can attack again

where am I supposed to add this script at?

btw, not means debounce == false in this script

ok it’ll change those things thanks

Good luck with the script then

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.

should my script look like this?

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)

Little nitpick: wait() is deprecated, change it to task.wait().

Oh and why do you have two activated events?

i changed it sorry i forgot about that

sorry my mistake ill fix that now

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)

This is how I think it should look.

this is the fixed version

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)

thanks you found the solution to my problem

but the only issue is that it does not wait 5 seconds it waits like 1 second

Hammer.Activated:Connect(function()
	if debounce then return end
        debounce = true
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://121844901937516"
	
	local animationTrack = animator:LoadAnimation(animation)
	animationTrack:Play()

	task.wait(cooldown)
        debounce = false
end)

Hammer.Unequipped:Connect(function()
	animationTrack:Stop()
	task.wait(cooldown)
        debounce = false
end)

like that indeed
charrrrrrrrrr