Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a cooldown for this animation script?

Asked by 7 years ago
Edited 7 years ago

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)

0
btw, the "if input" part is UNDER the Processed Event part. my copy and paste doesnt space it right. Sushionti 2 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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)
0
If my answer helped, please remember to click "Accept Answer" below the answer. TheHospitalDev 1134 — 7y
0
I'm sorry, it didn't work. Sushionti 2 — 7y
0
It DID work, just doesn't work in Studio. lol isnt that weird???? only works in the game mode. THANK YOU! Sushionti 2 — 7y
Ad

Answer this question