luaroblox

Script doesn't change the model based on time


I'm working on an automatic Street Light that is supposed to change based on the time. Here is the code:

--------------------------------------------
-----------Street Light Script!-------------
-----------Made by multiplemark-------------
--------------------------------------------
local ControlledByGameTime = true -- Setting to true makes it so that the lights 
activate only during the selected time.
local TurnLightsOnAt = 20 * 60 -- Turns lights on at 8 P.M. by default.
local TurnLightsOffAt = 8 * 60 -- Turns lights off at 8 A.M. by default.
local Lights = script.Parent.PointLight.Enabled
local LightBlock = script.Parent
if ControlledByGameTime == true then
    while true do
        local CurrentTime = game.Lighting:GetMinutesAfterMidnight()
        if CurrentTime >= TurnLightsOffAt then 
            Lights = false
            LightBlock.Material = "SmoothPlastic"
            LightBlock.Brick = 163,162,165
        end
        if CurrentTime >= TurnLightsOnAt then
            Lights = true
            LightBlock.Material = "Neon"
            LightBlock.Brick = 255,255,0
        end
    end
else
    Lights = true
    LightBlock.Material = "Neon"
    LightBlock.Color = 255,255,0
end

What it is supposed to do is check for the time, and if it meets the requirements defined, change the material and color of a model, and also enabling/disabling PointLight.


Solution

  • I think the issue is your if statements. You're evaluating if it's after 8 pm after you evaluate if it's 8 am. Since 8 pm (or 20 in 24-hour format) comes after 8 am (or 8 24-hour format), if a value if greater than 8 pm, it's also greater than 8 am.

    What you could do is just have an equals statement instead of a greater than. So something like:

    if (time==TURNOFFLIGHTS)
        turnofflights ();
    
    else if (time==TURNONLIGHTS)
        turnonlights ();
    

    That way, it only turns off the lights at the specific moment that night begins and only turns them on the moment day begins.