I'm trying to make it so when a player clicks a button the button waits for 3 seconds before it can work again but every time I tried to do it if the player spam clicks it the function still gets spammed, is there any way to add a cooldown to when the button can be pressed?
Discord: Unknown Intelligence#6569
01 | local bool = true |
02 |
03 | local Players = game:GetService( "Players" ) |
04 |
05 | Players.PlayerAdded:Connect( function (player) |
06 | local light = game:GetService( "Lighting" ) |
07 | light.TimeOfDay = 1 |
08 | light.FogEnd = 50 |
09 | light.FogStart = 20 |
10 | light.Brightness = 0 |
11 | light.FogColor = Color 3. new( 0 , 0 , 0 ) |
12 |
13 | end ) |
14 |
15 |
In the scripting world, we call this "debounce". Don't ask me why, the Code Gods just made it like that. Before your function, add a variable called "debounce" and set it to false. Then when the function is ran, set the debounce as true with a wait time of three seconds (or whatever you want). It'll look something like what I have below.
01 | local debounce = false |
02 |
03 | function onMouseClick() |
04 | if not debounce then |
05 | debounce = true |
06 | if bool = = true then |
07 | local light = game:GetService( "Lighting" ) |
08 | script.Lightson:Play() |
09 | light.TimeOfDay = 12 |
10 | light.FogEnd = 1000000 |
11 | light.FogStart = 0 |
12 | bool = false |
13 | else if bool = = false then |
14 | local light = game:GetService( "Lighting" ) |
15 | script.Lightsoff:Play() |
Sorry if the script is badly formatted and organized, writing this on mobile xD.