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

How Do I Fix This Broken Click Detector Debounce Script?

hello, I tried to make a click detector part that makes it so when you click it, it changes from day to night and when you click it again it changes from night to day. but for some reason it didn't work and I couldn't seem to find the problem.

here's my code:

local db = false

script.Parent.ClickDetector.MouseClick:Connect(function()	
	if db == false then
		game.Lighting.ClockTime = 0
		db = true
	end
	if db == true then
		db = false
		game.Lighting.ClockTime = 14
	end
end)

Any help would be greatly appreciated!

1 answer

You'll want to use elseif instead of separate if statements, the first statement makes it so that the second one always fires. Ex:

local db = false

script.Parent.ClickDetector.MouseClick:Connect(function()   
    if db == false then
        game.Lighting.ClockTime = 0
        db = true
    elseif db == true then
        db = false
        game.Lighting.ClockTime = 14
    end
end)

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