How to Make a Cooldown for my ClickDetector

Hello ! I am trying to make a button that gets disabled 30 seconds once it is clicked, basically making a cooldown. This button is NOT a GUI, it is a part. I tried destroying the part and recreating it after, which did not work…

Here is some of the code I made:
For a little bit of context: ButtonClick is a variable which IS the ClickDetector

ButtonClick.MouseClick:Connect(CeilingFalling)

if ButtonClick.MouseClick then
	ButtonClick:Destroy()
	wait(30)
	ButtonClick:Create()
end
5 Likes

Just make like this:

ButtonClick.MouseClick:Connect(CeilingFalling)

ButtonClick.MouseClick:Connect(function()
if not debounce then -- Checking if no one's clicked in the last 30 seconds because of the debounce!
CeilingFalling()
debounce = true -- Setting to true for they can't click again!
delay(30, function()
debounce = false -- So basically, waiting 30 seconds, and setting to false, for they can click again!
end)
end
end
end)

Adding a variable or debounce, and checking if it’s false, will make what you want
if you make a variable

local debounce = false

if debounce == false then
end

As debounce is fale, the code will work, else if debounce is == true then, it’s not going to work, you can change this to: if debouce == true then, but the debounce variable must be true to make it work.

2 Likes

This doesn’t seem to work… here is what i did:

ButtonClick.MouseClick:Connect(CeilingFalling)

ButtonClick.MouseClick:Connect(function()
	if not debounce then
		CeilingFalling()
		debounce = true
		delay(30, function()
			debounce = false
		end)
	end	
end)

I also added the debounce variable in the begining

local debounce = false
2 Likes
local debounce = false

ButtonClick.MouseClick:Connect(function()
	if not debounce then
		CeilingFalling()
		debounce = true
		delay(30, function()
			debounce = false
		end)
	end	
end)

This should work perfectly. test it.

2 Likes

It still doesn’t work with this code… This is quite strange…

1 Like

Do you have ButtonClick defined?

1 Like

Yes.

local Button = game.Workspace.Button
local ButtonClick = Button.ClickDetector
1 Like

It’s working, just CeliningFalling what is?

1 Like

This is CeilingFalling:

function CeilingFalling()
	
	NeonPart1.BrickColor = BrickColor.Red()
	NeonPart2.BrickColor = BrickColor.Red()
	NeonPart3.BrickColor = BrickColor.Red()
	NeonPart4.BrickColor = BrickColor.Red()
	
	Part1.BrickColor = BrickColor.new("Salmon")
	Part2.BrickColor = BrickColor.new("Salmon")
	Part3.BrickColor = BrickColor.new("Salmon")
	Part4.BrickColor = BrickColor.new("Salmon")
	
	Ceiling.BrickColor = BrickColor.new("Daisy orange")
	
	wait(3)
	
	Ceiling.BrickColor = BrickColor.Red()
	
	local Anim = {Position = Vector3.new(-41.5, 59.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(5)
	
	local Anim = {Position = Vector3.new(-41.5, 709.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(5.5)
	
	NeonPart1.BrickColor = BrickColor.new("Lime green")
	NeonPart2.BrickColor = BrickColor.new("Lime green")
	NeonPart3.BrickColor = BrickColor.new("Lime green")
	NeonPart4.BrickColor = BrickColor.new("Lime green")
	
	Part1.BrickColor = BrickColor.new("Mint")
	Part2.BrickColor = BrickColor.new("Mint")
	Part3.BrickColor = BrickColor.new("Mint")
	Part4.BrickColor = BrickColor.new("Mint")
	
	Ceiling.BrickColor = BrickColor.new("Pastel green")
	
end

It works just fine when I call it but somehow doesn’t work with debounce…

1 Like

it should… wdym? is ceiling falling over the other part of code?

1 Like

it should work although it doesn’t…
Here is my entire script:

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(5, Enum.EasingStyle.Linear)

local Ceiling = game.Workspace.TheUltimateCeiling
local Borders = game.Workspace.Borders

local Button = game.Workspace.Button
local ButtonClick = Button.ClickDetector

local NeonPart1 = Borders.NeonPart1
local NeonPart2 = Borders.NeonPart2
local NeonPart3 = Borders.NeonPart3
local NeonPart4 = Borders.NeonPart4

local Part1 = Borders.Part1
local Part2 = Borders.Part2
local Part3 = Borders.Part3
local Part4 = Borders.Part4

function CeilingFalling()
	
	NeonPart1.BrickColor = BrickColor.Red()
	NeonPart2.BrickColor = BrickColor.Red()
	NeonPart3.BrickColor = BrickColor.Red()
	NeonPart4.BrickColor = BrickColor.Red()
	
	Part1.BrickColor = BrickColor.new("Salmon")
	Part2.BrickColor = BrickColor.new("Salmon")
	Part3.BrickColor = BrickColor.new("Salmon")
	Part4.BrickColor = BrickColor.new("Salmon")
	
	Ceiling.BrickColor = BrickColor.new("Daisy orange")
	
	wait(3)
	
	Ceiling.BrickColor = BrickColor.Red()
	
	local Anim = {Position = Vector3.new(-41.5, 59.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(5)
	
	local Anim = {Position = Vector3.new(-41.5, 709.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(5.5)
	
	NeonPart1.BrickColor = BrickColor.new("Lime green")
	NeonPart2.BrickColor = BrickColor.new("Lime green")
	NeonPart3.BrickColor = BrickColor.new("Lime green")
	NeonPart4.BrickColor = BrickColor.new("Lime green")
	
	Part1.BrickColor = BrickColor.new("Mint")
	Part2.BrickColor = BrickColor.new("Mint")
	Part3.BrickColor = BrickColor.new("Mint")
	Part4.BrickColor = BrickColor.new("Mint")
	
	Ceiling.BrickColor = BrickColor.new("Pastel green")
	
end

local debounce = false

ButtonClick.MouseClick:Connect(function()
	if not debounce then
		CeilingFalling()
		debounce = true
		delay(30, function()
			debounce = false
		end)
	end	
end)

and here is a “Default Settings” script I made:

local Ceiling = game.Workspace.TheUltimateCeiling
local Borders = game.Workspace.Borders

local NeonPart1 = Borders.NeonPart1
local NeonPart2 = Borders.NeonPart2
local NeonPart3 = Borders.NeonPart3
local NeonPart4 = Borders.NeonPart4

local Part1 = Borders.Part1
local Part2 = Borders.Part2
local Part3 = Borders.Part3
local Part4 = Borders.Part4



Ceiling.CanCollide = false

NeonPart1.BrickColor = BrickColor.new("Lime green")
NeonPart2.BrickColor = BrickColor.new("Lime green")
NeonPart3.BrickColor = BrickColor.new("Lime green")
NeonPart4.BrickColor = BrickColor.new("Lime green")

Part1.BrickColor = BrickColor.new("Mint")
Part2.BrickColor = BrickColor.new("Mint")
Part3.BrickColor = BrickColor.new("Mint")
Part4.BrickColor = BrickColor.new("Mint")
	
Ceiling.BrickColor = BrickColor.new("Pastel green")
Ceiling.Position = Vector3.new(-41.5, 709.5, 19.5)
3 Likes

It could possibly be because you have the debounce after calling the function, so the cool-down doesn’t trigger until after the CeilingFalling() function has already been called x amount of times. Try switching them around like this:

ButtonClick.MouseClick:Connect(function()
	if not debounce then
		debounce = true --- I switched this one.
		CeilingFalling() --- With this one.
		delay(30, function()
			debounce = false
		end)
	end	
end)
4 Likes

Thank you and @varjoy so much ! This worked !

1 Like

@BruiseIgnio and @varjoy. Nevermind lol… It works but once I pressed the button I cannot press it again… Even after waiting 30 seconds.

2 Likes

The delay(function() method is unreliable, because it’s not very accurate, wait() should work better.

local debounce = false

ButtonClick.MouseClick:Connect(function()
	if debounce == false then
        debounce = true 
--[[ ALWAYS put debounces before calling any functions 
    or executing lines of code, because sometimes the 
    lines of code and functions take a while to execute, 
    therefore making people able to click it more than 
    once before waiting
--]]
		CeilingFalling()
		wait(30)
		debounce = false
	end	
end)

The reason it doesn’t work after 30 seconds is that you have wait() commands that yield the thread in the CeilingFalling() function. So, instead of waiting 30 seconds, it waits 30 seconds plus the length of waits in the function. To counter this, you can use coroutines:

so, instead of doing this:

local function CeilingFalling()

end

CeilingFalling()

do:

local CeilingFalling = coroutine.create()
    -- Code here
end

coroutine.resume(CeilingFalling)

Briefly what coroutines do is they basically add a script to a script, so coroutine.create basically creates a script, and coroutine.resume runs any code within that script.

11 Likes