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
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)
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)