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

How do I make a cooldown for this animation script?

So this is a script that every time you press "M" You will get a slash animation from your character. What I would LOVE to know (AND WOULD SAVE MY GAME IDEA :D) Is how I would put a cooldown for it?

---Notes------------------------------- It's a Local Script It's INSIDE a StarterCharacter

function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.M then

local animation = game.Workspace.Slash

local anim = script.Parent.Humanoid:LoadAnimation(animation)

anim:Play()

end end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 comment

0

btw, the "if input" part is UNDER the Processed Event part. my copy and paste doesnt space it right.

1 answer

✓ Accepted answer

A lovely thing called a Debounce

A debounce basically means "If soAndSo is not this don't continue. But if it is, continue and wait until I am re-enabled." Or as the wiki states, A debounce system is a set of code that keeps a function from running too many times. It comes from the idea of mechanical switch bounce, where a switch bounces when pushed, creating multiple signals.

To fix your code

local debounce = false --Define it, can be named anything

function onKeyPress(inputObject, gameProcessedEvent) 
	if not debounce then --If the debounce is set to "false"
		if inputObject.KeyCode == Enum.KeyCode.M then
			debounce = true --Set it to true,
			local animation = game.Workspace.Slash
			local anim = script.Parent.Humanoid:LoadAnimation(animation)
			anim:Play()
			wait(5) --"Cooldown"
			debounce = false --Set it to false,
		end 
	end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

3 comments

0

If my answer helped, please remember to click "Accept Answer" below the answer.

0

I'm sorry, it didn't work.

0

It DID work, just doesn't work in Studio. lol isnt that weird???? only works in the game mode. THANK YOU!

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