Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do I Fix This Broken Click Detector Debounce Script?

Asked by
bx3t 2
2 years ago

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

Log in to vote
2
Answered by
ultrabug 306 Moderation Voter
2 years ago

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)
Ad

Answer this question