我正在研究一种应该根据时间改变的自动路灯。这是代码:
--------------------------------------------
-----------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
它应该做的是检查时间,如果它符合定义的要求,更改模型的材料和颜色,以及启用/禁用PointLight。
答案 0 :(得分:0)
我认为问题是你的if语句。在评估是否是上午8点之后,您将在晚上8点之后评估它是否正常。由于晚上8点(或24小时格式的20)在早上8点(或8小时24小时格式)之后出现,如果值大于晚上8点,那么它也大于上午8点。
你能做的只是有一个等于语句而不是大于。如下所示:
if (time==TURNOFFLIGHTS)
turnofflights ();
else if (time==TURNONLIGHTS)
turnonlights ();
这样,它只会在当晚开始的特定时刻关闭灯光,并且只会在当天开始时关闭它们。