Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need my script to have a working cool down, but I don't know how to do that. Help?

gcount = 0
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local zanim = Instance.new("Animation")
zanim.AnimationId = "http://www.roblox.com/asset/?id=1556407132"

local diveanim = Instance.new("Animation")
diveanim.AnimationId = "http://www.roblox.com/asset/?id=1556546195"



mouse.KeyDown:connect(function(key)
	if key == "z" then 
		if gcount >4 then
			local playAnim = humanoid:LoadAnimation(diveanim)
			playAnim:Play()
			gcount = 0			
		else
			local playAnim = humanoid:LoadAnimation(zanim)
			playAnim:Play()
			gcount = gcount + 1		
		wait(1)
		
		
			
	if gcount >4 then
		local playAnim = humanoid:LoadAnimation(diveanim)
		playAnim:Play()
		gcount = 0
		
		

	end	
		
		end
	end
end)

If the user presses the animation too often, they should be able to fall over.

There's just something I'm doing wrong, the animation can be 'restarted' while they are using it, and I thought I could fix it by using wait(animation length) but obviously not.

It plays the original animation but I don't know how to make it unable to play the same animation again, like having a cooldown on the animation

I'm aware it's really inefficient as well

;)

Thanks

2 comments

0

Debounce?!

1 answer

Events triggers instantly if the condition is met (Even if its still running some code inside that function). Adding on to what Redcommander has said, yes you will need a debouncer. Incase you didn't know what a debounce is, its basically a variable used to prevent something from happening too quickly(I always use them on "Touched" event). consider the following as how a debouncer should work:

local debounce = true --name it whatever you want
	  
mouse.KeyDown:connect(function(key)
	if key == "z" and debounce ==true then
		debounce = false
		--Do whatever you want--
		wait(1) -- or you can wait for your animation to end
		debounce = true
	end
end)

Notice how the above code forces the keydown event to only be available after waiting for 1 second? That's the example and purpose of debounce.

This discussion is preserved as it was. New answers, comments, and votes are closed.