Help with adding cooldown?

Hello, so I have a simple script but nothing will work when I try to add a cool down to it. How am I able to add one?

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()


script.Parent.Activated:Connect(function()
	
	script.Parent.TaserClicked:FireServer(mouse.Hit)
	script.Parent.Sound:Play()
end)

You can make a very simple cooldown system by using a debounce. Make two variables called debounce and cooldown. When the debounce is true, we do nothing and set the debounce to true. We then wait cooldown seconds and then set the debounce to false again. This will make the script run once, stop working, and start going again when the cooldown time has passed.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local cooldown = 2 -- seconds in cooldown
local debounce = false -- keeps track if the cooldown is on or off

script.Parent.Activated:Connect(function()
    if debounce == true then return end -- stop and do nothing if its on cooldown
    debounce = true -- set the debounce to true so the line of code before this stops the code under this from running

	script.Parent.TaserClicked:FireServer(mouse.Hit)
	script.Parent.Sound:Play()

    task.wait(cooldown) -- wait for the cooldown
    debounce = false -- set the debounce to false again so our script can now run
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.