So I have a street light that I want to turn on and off at certain times but it's not really working. I have made a simple day and night script that increases the Lightings TimeOfDay by 1. Anyways, this Street Light never goes on. By default it is turned off. Here is the code I got so far. The output has shown nothing wrong with it.
local Lightime = script.Parent.PointLight.Enabled function Daynight(hit) if game.Lighting.TimeOfDay >= 18 then if Lightime == false then Lightime = true elseif game.Lighting.TimeOfDay >= 6 then if Lightime == true then Lightime = false end end end end
Should I just add a loop so it is constantly checking or what?
local Lightime = script.Parent.PointLight.Enabled function Daynight() if game.Lighting.TimeOfDay >= '18:00:00' then if Lightime == false then Lightime = true end elseif game.Lighting.TimeOfDay >= '06:00:00' then if Lightime == true then Lightime = false end end end game.Lighting.Changed:connect(Daynight)
There's two ways to notice whether a property in the Lighting has changed. You can use the event ".Changed" or ".LightingChanged". With the difference being, if you set the property TimeOfDay to and integer .LightingChanged will fire twice because you change it to an integer and then roblox changes it to the correct stringformat: 'HH:MM:SS'. That's why I chose .Changed here, because the less code you execute, the better.
I also cleaned up your if statements, you seemed to have a few unnecessary ends in there. A great way to prevent that from happening is to keep everything from if to then on the same line. Everything inside the if statement gets an extra indent, just like I did in my code.
Furthermore, I have made sure your if statements compare to the correct string, rather than to an integer. Roblox will automatically convert an integer like 6 to the string "6", however the string "6" does not equal to "06:00:00", which is the format in which TimeOfDay is saved.
I hope this helps, let me know if you run into any trouble!