Cooldowns on Enum.UserInputType.MouseButton1 how?

Hi, how can i get a Cooldown on this, just a simple wait(0.2) doesnt work, it just delays it so

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then

	end
end)

what you would want to use is a simple method that many have used for this sorta thing, we call it, the “debounce”
basically its just a variable that gets set to true, so then we know hey we cant do this, then after X amount of time we set debounce back to false.
Example with your code:

local debounce = false

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if debounce then return end -- If debounce is set to true, then we cant do this, so we use the return method to stop the code after this point from running
        debounce = true -- sets debounce to true, meaning the function can no longer run until its set back to false
         --- Do whatever
        task.wait(X) -- Replace X with the time you want to wait.
        debounce = false -- sets debounce to false, now the function can run again!
	end
end)
1 Like

Yesss, this works thank you alot, but im still wondering why not just use the wait function, why specifically task.wait, is task.wait more precise?

Don’t always debounce. That’s bad practice.

I’d suggest you use an actual cooldown instead like in this example:

local COOLDOWN_TIME = 0.2
local COOLDOWN = 0

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and COOLDOWN <= tick() then
		COOLDOWN = tick() + COOLDOWN_TIME
		
		-- Do what you want to do here
	end
end)

Best regards,
Pinker

2 Likes

holy thats a thing!
This looks good, but cause Im curious, and nerdy, how is it bad practice? Is there a downside to using a debounce? I need that statistics!! lol

1 Like

Yes this works alot better, debounce had some delay of like ~0.2-0.3 seconds but this works amazing, thank you both alot

1 Like

It just doesn’t keep the thread alive for the duration of the cooldown.
Mostly this way is just cleaner and more readable though tbh.


Best regards,
Pinker

Ok, thanks for the info!

1 Like