Clock time check doesnt work

so i made a day and night cycle (kinda bad) that kinda works it makes the clock time go up but i have some checks that change the fog automatically when the clock time reaches specific number

local DayState = Instance.new("Folder", game)
DayState.Name = "DayInfo"

local isday = Instance.new("BoolValue", DayState)
isday.Name = "IsDay"

local isnight = Instance.new("BoolValue", DayState)
isday.Name = "IsNight"

-- // starts here

local rgb = Color3.fromRGB

local states={
    ["day"]={
        ["ambient"]  = rgb(168,168,168);
        ["outdoor"]  = rgb(168,168,168);
        ["fogcolor"] = rgb(144,144,144);
        ["fogend"]   = 70;
        ["fogstart"] = -5
    };
    ["night"] = {
        ["ambient"]  = rgb(0,0,0);
        ["outdoor"]  = rgb(0,0,0);
        ["fogcolor"] = rgb(65, 66, 94);
        ["fogend"]   = 70;
        ["fogstart"] = -5
    }
}

local dayTime = 6
local nightTime = 18

while task.wait(0.01) do
    local lighting = game:GetService("Lighting")
    local rate = 0.001
    
    if lighting.ClockTime == dayTime or (lighting.ClockTime > dayTime or lighting.ClockTime < nightTime) == true then
        lighting.Ambient = states["day"]["ambient"]
        lighting.OutdoorAmbient = states["day"]["outdoor"]
        lighting.FogColor = states["day"]["fogcolor"]
        lighting.FogEnd = states["day"]["fogend"]
        lighting.FogStart = states["day"]["fogstart"]
        isnight.Value = false
        isday.Value = true
        
    elseif lighting.ClockTime == nightTime or (lighting.ClockTime > nightTime or lighting.ClockTime < dayTime) == true then
        lighting.Ambient = states["night"]["ambient"]
        lighting.OutdoorAmbient = states["night"]["outdoor"]
        lighting.FogColor = states["night"]["fogcolor"]
        lighting.FogEnd = states["night"]["fogend"]
        lighting.FogStart = states["night"]["fogstart"]
        isnight.Value = true
        isday.Value = false
    end

    lighting.ClockTime += rate
end

and the clock time goes up ok but the check that is lighting.ClockTime doesnt detect it and it will stay on the same day state as always (default one cuz i manually set it)

idk if its the way i tried to check it but it should work to my knowledge but it doesnt

1 Like

Your states are functions instead of Color3’s:
Color3.fromRGB()

Also use plain text instead of strings for your table

1 Like

The logic in your if statements doesn’t correctly capture the transitions between day and night. Specifically, the condition lighting.ClockTime == dayTime won’t trigger because lighting.ClockTime is unlikely to match dayTime exactly due to the small increment you’re adding (rate = 0.001) each frame.

while task.wait(0.01) do
    local lighting = game:GetService("Lighting")
    local currentTime = lighting.ClockTime
    
    if currentTime >= dayTime and currentTime < nightTime then
        -- Daytime
        lighting.Ambient = states["day"]["ambient"]
        lighting.OutdoorAmbient = states["day"]["outdoor"]
        lighting.FogColor = states["day"]["fogcolor"]
        lighting.FogEnd = states["day"]["fogend"]
        lighting.FogStart = states["day"]["fogstart"]
        isnight.Value = false
        isday.Value = true
        
    else
        -- Nighttime
        lighting.Ambient = states["night"]["ambient"]
        lighting.OutdoorAmbient = states["night"]["outdoor"]
        lighting.FogColor = states["night"]["fogcolor"]
        lighting.FogEnd = states["night"]["fogend"]
        lighting.FogStart = states["night"]["fogstart"]
        isnight.Value = true
        isday.Value = false
    end

    lighting.ClockTime += rate
end
3 Likes

The issue lies in your conditional statements and the way you are checking the clock time property. Try this:

local DayState = Instance.new("Folder", game)
DayState.Name = "DayInfo"

local isday = Instance.new("BoolValue", DayState)
isday.Name = "IsDay"

local isnight = Instance.new("BoolValue", DayState)
isday.Name = "IsNight"

-- // starts here

local rgb = Color3.fromRGB

local states={
    ["day"]={
        ["ambient"]  = rgb(168,168,168);
        ["outdoor"]  = rgb(168,168,168);
        ["fogcolor"] = rgb(144,144,144);
        ["fogend"]   = 70;
        ["fogstart"] = -5
    };
    ["night"] = {
        ["ambient"]  = rgb(0,0,0);
        ["outdoor"]  = rgb(0,0,0);
        ["fogcolor"] = rgb(65, 66, 94);
        ["fogend"]   = 70;
        ["fogstart"] = -5
    }
}

local dayTime = 6
local nightTime = 18

while task.wait(0.01) do
    local lighting = game:GetService("Lighting")
    local rate = 0.001

    lighting.ClockTime += rate

    if lighting.ClockTime >= dayTime and lighting.ClockTime < nightTime then
        lighting.Ambient = states["day"]["ambient"]
        lighting.OutdoorAmbient = states["day"]["outdoor"]
        lighting.FogColor = states["day"]["fogcolor"]
        lighting.FogEnd = states["day"]["fogend"]
        lighting.FogStart = states["day"]["fogstart"]
        isnight.Value = false
        isday.Value = true
    elseif lighting.ClockTime >= nightTime or lighting.ClockTime < dayTime then
        lighting.Ambient = states["night"]["ambient"]
        lighting.OutdoorAmbient = states["night"]["outdoor"]
        lighting.FogColor = states["night"]["fogcolor"]
        lighting.FogEnd = states["night"]["fogend"]
        lighting.FogStart = states["night"]["fogstart"]
        isnight.Value = true
        isday.Value = false
    end
end
1 Like

i fixed now it sets the settings ty (also thanks to scorp and kleyn)