How do I add this a cooldown

Hi,

I wanted to add a cooldown to this uis the I made
I have no experience on adding a cooldown to it

here is my script:

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.P then
		clicks = clicks + 1 -- this detects if player clicks the input twice or more
		if clicks == 1 then
        --code
        if click == 2 then
        -- code

thanks in advance :smiley:

2 Likes

You need to use Debounce for this.

Debounce

im gonna test it outβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Ž

1 Like

I have no idea on how to put it in my script
and yes I already now what debounce is but the
problem is I don’t know how to use it

local db = false

UIS.InputBegan:Connect(function()
        if not db then
            db = true
            if input.KeyCode == Enum.KeyCode.P then
                clicks = clicks + 1 -- this detects if player clicks the input twice or more
                if clicks == 1 then
                    --code
                elseif clicks == 2 then
                --code
                end
            end
            game:GetService("RunService").Heartbeat:Wait() -- You can use wait() here
            db = false
        end
    end)
2 Likes

Here I rewrote the piece of code, cleaned it up a little bit and added the debounce/cooldown! Hope this helps!

local db = false
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.P and db == false then
		db = true
		clicks = clicks + 1 -- this detects if player clicks the input twice or more
		if clicks == 1 then
			-- code
		elseif clicks == 2 then
			-- code
		end
		delay(1,function() -- Change the 1 to a number of your choice
			db = false
		end)
	end
end)
2 Likes

sorry for the late response but thanks for all the solutions
I should check out what delay means