What is wrong with this script?

Hello!

My goal is to have a part with a spotlight attached to it, where during the day the light is off, but at night the light is on.

The problem is, the light stays on during the day as well. There are no visible errors in the script and no error messages. Why isn’t it working?

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

It seems like the issue is with the conditions in your if statements. The current code sets the part material to Plastic and disables the spotlight when the game’s time is greater than 6 am, and sets the part material to Neon and enables the spotlight when the game’s time is greater than 6 pm. However, once the game’s time passes 6 am, the spotlight will remain off until the game’s time passes 6 pm. This means that during the daytime, the spotlight will remain off, but the material of the part will still be set to Plastic.

To fix this issue, you can update the conditions in your if statements to check if the current time is between 6 pm and 6 am. Here’s an updated version of your code that should work as intended:

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

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

In this updated code, the spotlight will be enabled and the part material will be set to Neon when the game’s time is between 6 pm and 6 am, and the spotlight will be disabled and the part material will be set to Plastic during the daytime.

Thanks for the help! I’ll check to see if it works tomorrow!