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
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.
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
ButtonClick.MouseClick:Connect(function()
if not debounce then
CeilingFalling()
debounce = true
delay(30, function()
debounce = false
end)
end
end)
It could possibly be because you have the debounceafter 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)
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.