How do I add a cooldown to this?

How could I add a cooldown to this animation system that fires on tool equip?

Tool.Equipped:Connect(function()
	ToolEquipAnimation:Play()
		
	ToolEquipAnimation.Stopped:Connect(function()
		ToolIdleAnimation:Play()
	end)
end)

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

Just add a Debouce variable

Local Debounce = false

Tool.Equipped:Connect(function()
	If Debounce then return end --if Debounce value is true then it cant be used so we will stop the function from running
	
	Debounce = true --setting it to true to add a coooldown
	
	ToolEquipAnimation:Play()
		
	ToolEquipAnimation.Stopped:Connect(function()
		Debounce = false --after the animation is done we set it to false so it doesn't stop th3 function
		ToolIdleAnimation:Play()
	end)
end)
--i don't think you need this part but you can keep it if you want to, wont change alot (i think)
Tool.Unequipped:Connect(function()
	for _, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		if AnimationTrack.Name == "R15ToolIdle" or AnimationTrack.Name == "R6ToolIdle" then
			AnimationTrack:Stop()
		end
	end
end)
1 Like

You can set the Debounce to false out of the function to avoid any bugs but it will need a wait() or a delay() and you will need the time needed for the animation to fully finish

Should I put the task.wait() after Debounce = true?

I am having a brain moment today

1 Like

If you dont want to to be after the animation is done then at the end of that function use wait(number) Debounce = false or use delay its pretty advanced but simple, another way is use a while loop NOT in the function and it checks if Debounce is true and after a specific time it makes it true, or you can just leave it as it is .-.

I probably should have said this. I basically want the player to have a cooldown before equipping the tool again.

Example:

When the player unequips the tool it has a cooldown until you can equip it again. I am having a massive brain moment so I am not sure how to write it out.

Ooooh so you cant equip the tool itself, i thought it was an animation thing, lemme re-write the code for you

1 Like

Okay it will be the same thing, except if the Debounce is true we will force unequip the tool using Humanoid:UnequipTools() which is a function made by roblox

local Debounce = false -- i had local capital last script, sry about that but im writing with my phone :-:

Tool.Equipped:Connect(function()
	if Debounce then return end --if Debounce value is true then it cant be used so we will stop the function from running
	
	ToolEquipAnimation:Play()
		
	ToolEquipAnimation.Stopped:Connect(function()
		ToolIdleAnimation:Play()
	end)
end)

Tool.Unequipped:Connect(function()
	Debounce = true
	
	delay(1 , function() --change 1 to the delay you want, delay to not yield the whole script, it delays the things inside the function ONLY
		Debounce = false
	end)
	
	for _, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		if AnimationTrack.Name == "R15ToolIdle" or AnimationTrack.Name == "R6ToolIdle" then
			AnimationTrack:Stop()
		end
	end
end)
1 Like

I did alot of miss-spells bit im writing on phone so sry about that i fixed the ones i found, in the new script

1 Like

For some reason it doesn’t stop the tool from being equipped but it delays the animation.

1 Like

Sry but my one braincell forgot to unequip the tools .-. Lemme fix the code, is this a local or server script?

1 Like

Is this a local script or a “normal” script

This is the ending code, dumb mistakes ngl, you didnt reply telling me which type of script is it so this will work with both…

local Debounce = false 

Tool.Equipped:Connect(function()
	
	local Character = game.Players.LocalPlayer.Character
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	
	if Debounce then Humanoid:UnequipTools() return end --if Debounce value is true then it cant be used so we will stop the function from running and unequip the tools
	
	ToolEquipAnimation:Play()	
	ToolEquipAnimation.Stopped:Connect(function()
		ToolIdleAnimation:Play()
	end)
end)

Tool.Unequipped:Connect(function()
	Debounce = true
	
	delay(1 , function() --change 1 to the delay you want, delay to not yield the whole script, it delays the things inside the function ONLY
		Debounce = false
	end)
	
	for _, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		if AnimationTrack.Name == "R15ToolIdle" or AnimationTrack.Name == "R6ToolIdle" then
			AnimationTrack:Stop()
		end
	end
end)
1 Like

So sorry its for the Client so a LocalScript.

1 Like

Alr, i changed the script try it now

I am getting this error in output:

Im pretty sure its because it cant be done using local scripts so try using remote events so server script can unequip

So change humanoid:UnequipTools() to RemoteEvent:FireServer() and in a server script

RemoteEvent.OnServerEvent:Connect(function(Player)
	Player.Character.humanoid:UnequipTools()
end)
1 Like