我的 Roblox 脚本不会根据时间更改模型 (Roblox)

My Roblox Script doesn't change the model based on time (Roblox)

我正在研究一种自动路灯,它应该会根据时间而变化。这是代码:

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

它应该做的是检查时间,如果它符合定义的要求,则更改模型的material和颜色,以及enabling/disabling PointLight。

我认为问题出在您的 if 语句上。在评估是否是上午 8 点之后,您正在评估是否在晚上 8 点之后。由于晚上 8 点(或 24 小时格式的 20)在上午 8 点(或 8 24 小时格式)之后,如果某个值大于晚上 8 点,则它也大于上午 8 点。

您可以做的只是使用等于语句而不是大于语句。所以像:

if (time==TURNOFFLIGHTS)
    turnofflights ();

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

这样,它只在夜晚开始的特定时刻关灯,并只在白天开始的那一刻打开灯。