[SOLVED] Help With Light Script

Hello developers! I need help with this script. What I want to achieve is for this script to turn the lights off during the day and turn them back on during the night. Here is the script:

local Part = script.Parent
local pointLight = Part.PointLight

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

The problem is that when I first spawn in, the lights stay on. Is there a way to fix it? God bless you.

2 Likes

You are only considering when time is bigger than 6 and 18. You have to make another if statement that checks whether the time is earlier than 6 or not.

1 Like

Try using this code for your script:

local Lighting = game:GetService("Lighting")
local Part = script.Parent
local pointLight = Part:WaitForChild("PointLight")

while wait() do
	if Lighting:GetMinutesAfterMidnight() > 6 * 60 or Lighting:GetMinutesAfterMidnight() < 18 * 60 then
		Part.Material = Enum.Material.Plastic
		pointLight.Enabled = false

	elseif Lighting:GetMinutesAfterMidnight() > 18 * 60 or Lighting:GetMinutesAfterMidnight() < 6 * 60 then
		Part.Material = Enum.Material.Neon
		pointLight.Enabled = true
	end
end

Edit: Found a little mistake in the code, just fixed it.

1 Like

The and should be replaced with or on line 10, Lighting:GetMinutesAfterMidnight() cannot be bigger than 18*60 and smaller than 6*60 at the same time!

2 Likes

I just tested it out. It didn’t solve the problem : /
Edit: I am trying out what @WilliamAlezandro said

any errors or blank? (30 letters)

" ‘Lighting’ is not a valid Service name"

I just fixed that. Recopy the code and paste it in your script again see if it will work this time

1 Like

OK, this may be a silly question but are you sure the time actually goes past 6:00.

1 Like

It worked! Thank you very much!

1 Like

Yes, I am sure. I have a day and night cycle that works

Consider using Lighting.ClockTime, its a much better metric than the now deprecated “GetMinutesAfterMidnight”

local Lighting = game:GetService("Lighting")

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
  local t = Lighting.ClockTime

end)

The best part is, this is an event handler so you dont need a loop

2 Likes