How to make a cooldown?

I’m trying to make a script so when you press “f” you will do a punch animation
I Don’t know how to make a cooldown to this script, please help me:


The issue is in the Input function, I want to make it so after the 0.5 seconds you couldn’t punch

5 Likes

Simply use os.clock to create the debounce.

An example.

local debounce = os.clock()
local function punch()
    if os.clock() - debounce < 0.5 then — 0.5 is the cooldown time
        return
    end

    — rest of code
    debounce = os.clock() — set debounce time
end

— other code to activate the punch
11 Likes
local cooldown = false

And then under “wait(0.5)”
put

cooldown = false
4 Likes

Can you put it without the text please? it is kinda confusing with it

1 Like

tried this, didnt work, maybe something was wrong with the writing or something

1 Like

I have edited the script now try it

1 Like

Nothing changed, still no cooldown

1 Like

Maybe the debounce is not in the correct place?

1 Like

What’s the point of all these variables?

Which one of the variables is it

1 Like

Most of them are damage and animations that are in the script below it that isnt shown

1 Like

please use ``` to format your code as text so it’s easier to edit, anyway

local debounce = false

Input.InputBegan:Connect:(function(Input)
if key == Enum.KeyCode.F and debounce = false then
debounce = true
--write all your code here
wait(cooldown amount here)--replace cooldown amount here with how long you want the cooldown in seconds
debounce = false
end
end)
6 Likes

Do i need this:

local function punch()
    if os.clock() - debounce < 0.5 then
        return
    end

No you do not, that’s just another way to check for debounce, but make sure you keep all those variables like canpunch and d = true and damage = false and stuff

with the debounce false the character does not play the animation nor hit the other character.

if key == Enum.KeyCode.F and debounce = false then
1 Like

The way I typically create cooldown systems: You can try to follow this

local debounce = true -- enable it by default
local cooldown = 0.5 -- the amount of time to cooldown for

input.InputBegan:Connect(function(key, typing) -- define the second parameter for UserInputService.InputBegan (which is in-case the player is talking in chat)
    if typing then return end -- if the player is chatting then end the function
    local button = Enum.KeyCode.F -- the key that needs to be pressed

    if key.KeyCode == button then
       if debounce then -- check if debounce is enabled
          print("passed") -- print something to see if it's running
          debounce = false -- disable the debounce variable
                 -- code here
          wait(cooldown) -- wait for the amount of time the "cooldown" variable is
          debounce = true -- re-enable it
       end
    end
end)
3 Likes

Try this

local input = game:GetService("UserInputService") 
local anim
local h = script.Parent.Humanoid
local dmg = false
local d = true
local char = script.Parent
local canPunch = false
local anim
    debounce = os.clock() — set debounce time
end

Input.InputBegan:Connect(function(key) 
local f = key.KeyCode
if f == Enum.KeyCode.F and canPunch == false then
if d then
canPunch = true
d = false
dmg = true
anim = script.Parent.Humanoid:LoadAnimation(script.Animation):Play()
wait(0.5)
dmg = false
d = true
wait(2)
canPunch = false
end
end
7 Likes

sorry, I forgot to add two equal signs

if key == Enum.KeyCode.F and debounce == false then

So in the beginning of ur code do if canpunch then return end b

Thank you! this worked out well.

1 Like