Debounce Script Not Working?

Im making a script where it’ll start an alarm when you click the button. That part has been done but I don’t know how to use a debounce script properly because it’ll be annoying when someone spams the alarm.

if debounce then
	debounce = false
	
	task.wait(5)
	
	button.MouseClick:Connect(function()
		if not AlarmIsOn  then
			AlarmIsOn = true
			for index, descendants in pairs(descendants) do
				if descendants:IsA("PointLight") then	
					descendants.Color = Color3.fromRGB(255,1,1)
					descendants.Parent.BrickColor = BrickColor.new("Really red")

					script.Parent.Double:Play()

				end
			end
			GiveUI()
		else
			AlarmIsOn = false
			for index, descendants in pairs(descendants) do
				if descendants:IsA("PointLight") then
					descendants.Color = Color3.fromRGB(255,255,255)
					descendants.Parent.BrickColor = BrickColor.new("Institutional white")
					script.Parent.BreachAlarm.Looped = false
					script.Parent.Double:Play()
				end
			end
		end
	end)
	debounce = true
end

Try this

local debounce = true

button.MouseClick:Connect(function()
	if not AlarmIsOn  and debounce then
		debounce = false
		AlarmIsOn = true
		for index, descendants in pairs(descendants) do
			if descendants:IsA("PointLight") then	
				descendants.Color = Color3.fromRGB(255,1,1)
				descendants.Parent.BrickColor = BrickColor.new("Really red")

				script.Parent.Double:Play()

			end
		end
		GiveUI()
		task.wait(5)
		debounce = true
	elseif debounce then
		debounce = false
		AlarmIsOn = false
		for index, descendants in pairs(descendants) do
			if descendants:IsA("PointLight") then
				descendants.Color = Color3.fromRGB(255,255,255)
				descendants.Parent.BrickColor = BrickColor.new("Institutional white")
				script.Parent.BreachAlarm.Looped = false
				script.Parent.Double:Play()
			end
		end
		task.wait(5)
		debounce = true
	end
end)
1 Like

local debounce = 5

local canuse = true
script.Parent.MouseClick:Connect(function()
if canuse == true then
canuse = false
print(“y”)
wait(debounce)
canuse = true
end
end)

local debounce = false
	button.MouseClick:Connect(function()
		if not AlarmIsOn  then
			if debounce then return end -- We check if debounce is true only here since you probably want the cooldown only happen when player tries to turn it ON 
			AlarmIsOn = true
			for index, descendants in pairs(descendants) do
				if descendants:IsA("PointLight") then	
					descendants.Color = Color3.fromRGB(255,1,1)
					descendants.Parent.BrickColor = BrickColor.new("Really red")

					script.Parent.Double:Play()

				end
			end
			GiveUI()
		else
			debounce = true -- when alarm is turned off we set the debounce to true so you would have to wait before turning it on again
			AlarmIsOn = false
			for index, descendants in pairs(descendants) do
				if descendants:IsA("PointLight") then
					descendants.Color = Color3.fromRGB(255,255,255)
					descendants.Parent.BrickColor = BrickColor.new("Institutional white")
					script.Parent.BreachAlarm.Looped = false
					script.Parent.Double:Play()
				end
			end
			task.wait(5) -- wait 5 seconds or as long as you want
			debounce = false -- the alarm may be turned on again!
		end
	end)

It’s quite simple!
If you want the debounce to be applied both when alarm is turned on and off then you would have to do something like this:

local debounce = false
	button.MouseClick:Connect(function()
		if debounce then return end
		debounce = true
		if not AlarmIsOn  then
			AlarmIsOn = true
			--blah blah...--
		else
			AlarmIsOn = false
			---blah blah...---
		end
		task.wait(5)
		debounce = false
	end)
end
1 Like

yeah its really simple aaaaaaaaaaaaaaaaa