How to add cooldown to script?

Hey everyone! I would like to know how could I add a cooldown to this script I made:

Hover.MouseButton1Click:Connect(function()
	if WindowVisibility.Value == false then
		WindowVisibility.Value = true
		print("Opened")
	else
		WindowVisibility.Value = false
		print("Closed")
	end
end)

I tried to make something like this, but then the script stops working after the first input:

local debounce = false

--click
if debounce then return end
debounce = true
--function
wait(.2)
debounce = false
1 Like

If you don’t want to script to yield you can use task.spawn with the debounce.

1 Like
  1. Store the debounce as a variable
  2. If the debounce is active then return (make sure it’s in a function then
  3. Set the debounce to true after the return
  4. Do whatever you want.
  5. Have a cooldown time and wait here.
  6. set the debounce as false once more.
local db = false

-- click
if db then return end
db = true
... -- code here
task.wait(.2) -- cooldown time
db = false
4 Likes

Also if that solves it mark a reply as solution so that this post gets archived.

Yes! I was doing the cooldown stuff inside the if statements. Thank you so much for the help!

2 Likes

Yeah that also works, it looks alike:

local db = false

if not db then
   db = true
   ...
   task.wait()
   db = false
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.