Light Timing Not Working Properly

Hello!

I made a script that is supposed to turn a light on at night and off during the day. There are no noticeable errors in the script, but when I join the game during the daytime, the light is still on when it should be off.

This is the script:

local part = script.Parent
local spotLight = part.SpotLight

while true do
	wait(0.1)
	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
		part.Material = Enum.Material.Plastic
		spotLight.Enabled = false
	end
	if game.Lighting:GetMinutesAfterMidnight() > 18 *60 then
		part.Material = Enum.Material.Neon
		spotLight.Enabled = true
	end
end
1 Like

It works fine for me except for when the times are inbetween 12AM and 6AM. I added a extra conditional at the end of the second one to fix the issue.

local part = script.Parent
local spotLight = part.SpotLight

while true do
	wait(0.1)
	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
		part.Material = Enum.Material.Plastic
		spotLight.Enabled = false
	end
	if game.Lighting:GetMinutesAfterMidnight() > 18 *60 or game.Lighting:GetMinutesAfterMidnight() < 6 * 60 then
		part.Material = Enum.Material.Neon
		spotLight.Enabled = true
	end
end

hope that helps!