Lighting script doesn't update with change in time

You can write your topic however you want, but you need to answer these questions:
I tried making a script for a part that only emits lights between the evening and morning, but never at the daytime.

However, while the script seems to work in studio, the lights remain on during the day when loaded ingame even when I try updating the time of the day, only difference is that the script is stored in ReplicatedStorage, which is absolutely necessary for me.

I couldn’t find any solutions in the devforum, and the script used to work flawlessly months ago, so an update must’ve broke the script.

while true do
wait(1)
	if game.Lighting.TimeOfDay >= "18:00:00" then
		script.Parent.Light.Enabled = true
	elseif game.Lighting.TimeOfDay >= "07:00:00" then
		script.Parent.Light.Enabled = false
	end
end
2 Likes

oh ur if and elseif statements are the reason why its not working. try this, it may not work but hopefully it does… also task.wait is usually better than wait just a heads up. (-:

while true do
	task.wait(1)
	local TimeOfDay = game.Lighting.TimeOfDay
	
	if TimeOfDay >= "18:00:00" or TimeOfDay < "07:00:00" then
		script.Parent.Light.Enabled = true
	else
		script.Parent.Light.Enabled = false
	end
end
1 Like
if game.Lighting.TimeOfDay >= "18:00:00" then
elseif game.Lighting.TimeOfDay >= "07:00:00" then

You’re attempting to treat strings as numbers. :disappointed_relieved:
I recommend doing this instead:

local TheHour = game.Lighting.TimeOfDay:match("^(%d+):")
TheHour = tonumber(TheHour)
if TheHour >= 18 then
...
elseif TheHour >= 7 then
...
end

Alternatively, you could also use GetMinutesAfterMidnight.

local Lighting = game:GetService("Lighting")

while true do
	local CurrentTime = Lighting:GetMinutesAfterMidnight() / 60
	if CurrentTime >= 18 then
		script.Parent.Light.Enabled = true
	elseif CurrentTime >= 7 then
		script.Parent.Light.Enabled = false
	end
	wait(1)
end
2 Likes

Woke up thinking about my last reply. This one is way better.
5 seconds stall isn’t going to matter but will make this less cycle-greedy.
This one also checks to see if it needs to switch the light or not, instead of always updating.
(in case you ever wanted to do a folder with many lights, with one script)

local light = script.Parent.Light
local last

while task.wait(5) do
	local t = game.Lighting.ClockTime
	local switch = t >= 18 or t < 7
	if switch ~= last then
		light.Enabled = switch
		last = switch
	end
end

Also, none of these scripts will work out unless you have a script moving the time along…
Time isn’t moving on it’s own by default.

1 Like

Use the ClockTime property. TimeOfDay is a string value. (I use a translator, there may be errors in my message). Here’s the code, it works.

while true do
	wait(1)
	if game.Lighting.ClockTime >= 18 then
		script.Parent.Light.Enabled = true
	elseif game.Lighting.ClockTime >= 7 then
		script.Parent.Light.Enabled = false
	end
end
2 Likes

Didn’t work. For some reason, the script works when the part isn’t in replicated storage, but doesn’t when it is.

Didn’t work. I should note that I spawn the parts from Replicated Storage.

Did not fix it unfortunately, I have a script that allows me to change time manually, so don’t worry about that. I should note that I spawn the parts from Replicated Storage.

Didn’t work unfortunately. The parts are unloaded in ReplicatedStorage, then using a script, I load them in.

Mine is set up a bit odd because it’s like one step away from running all the lights.
If this is just one light you don’t need the last and switch logic, like this;

local light = script.Parent.Light

while task.wait(5) do
	local t = game.Lighting.ClockTime
	local mode = t >= 18 or t < 7 
	light.Enabled = mode 
end