Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I add a cooldown to my script? (Script attached)

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Tool = script.Parent

Tool.Equipped:Connect(function()
Mouse.Button1Down:Connect(function()
		
if Mouse.Target.Parent.Parent.Name == "F | Gate" then 
			
local HingeLeft = Mouse.Target.Parent.Parent.Left.LeftHingePart
local HingeRight = Mouse.Target.Parent.Parent.Right.RightHingePart

if HingeLeft.Parent.Parent.OpenvClosed.Value == "C" then 

game.ReplicatedStorage.Remotes.Gate:FireServer(HingeLeft,HingeRight)	

else
				
game.ReplicatedStorage.Remotes.GateC:FireServer(HingeLeft,HingeRight)	
				
end
end
end)
end)

I want my script to run the code and then wait 6 seconds before allowing it to be ran again.

2 answers

✓ Accepted answer

Try this:

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Tool = script.Parent
local debounce = false

Tool.Equipped:Connect(function()
Mouse.Button1Down:Connect(function()

if Mouse.Target.Parent.Parent.Name == "F | Gate" and debounce == false then 
debounce = true
local HingeLeft = Mouse.Target.Parent.Parent.Left.LeftHingePart
local HingeRight = Mouse.Target.Parent.Parent.Right.RightHingePart

if HingeLeft.Parent.Parent.OpenvClosed.Value == "C" then 

game.ReplicatedStorage.Remotes.Gate:FireServer(HingeLeft,HingeRight)  

wait(6)
 debounce = false  

else
debounce = true
game.ReplicatedStorage.Remotes.GateC:FireServer(HingeLeft,HingeRight)   
wait(6)
 debounce = false  
end
end
end)
end)

2 comments

0

Thank you! (:

0

No problem, just use true and false values, if debounce == false means if the cooldown = false then do it and then it sets cooldown to true, waits 6 seconds at the end of the script and sets it to false again, simple!

You can use a debounce. A debounce is, in simplest terms, an on/off switch for a function.

Considering this is a LocalScript, you can use a debounce variable. If you choose to debounce on a server script in the future, you will need to debounce using a different method (BoolValue, DataStore, etc).

To start, you will want to define the debounce variable outside of the Tool.Equipped and Mouse1Down events.

Second, inside the Mouse1Down event, you should check if the switch is "on." If the switch is on, then you don't want them to continue: if debounce then return end

If the switch is off, then you will want to turn it on before you run the rest of your code by setting the debounce variable to true.

Finally, once all your other code has been executed, proceed to add a wait(6), and simply set the switch back to off so that the next time Mouse1Down is fired it will be able to run.

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Tool = script.Parent
local debounce = false

Tool.Equipped:Connect(function()
    Mouse.Button1Down:Connect(function()
	if debounce then return end -- Check for on switch
	debounce = true -- Set to on if it is off

        if Mouse.Target.Parent.Parent.Name == "F | Gate" then 

            local HingeLeft = Mouse.Target.Parent.Parent.Left.LeftHingePart
            local HingeRight = Mouse.Target.Parent.Parent.Right.RightHingePart

            if HingeLeft.Parent.Parent.OpenvClosed.Value == "C" then 

                game.ReplicatedStorage.Remotes.Gate:FireServer(HingeLeft,HingeRight)    

            else

               game.ReplicatedStorage.Remotes.GateC:FireServer(HingeLeft,HingeRight)   

            end
        end
	wait(6)
	debounce = false -- Turn switch back off until next time
    end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.