Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

In my day/night script that also makes lights turn off and on, why ins't it working?

I have a day/night script, and then another script that's supposed to turn the lights off and make them plastic at 8AM. However, this isn't working. The light still stays the same. The day/night script works though. This is what I have so far.

lightPart = game.Workspace.LightPart
if game.Lighting:GetMinutesAfterMidnight() == 8 * 60 then
		lightPart.Material = Enum.Material.Plastic
		lightPart.PointLight.Enabled = false
	end
if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then
		lightPart.Material = Enum.Material.Neon
		lightPart.PointLight.Enabled = true
	end

1 comment

0

Added some information to my answer.

2 answers

✓ Accepted answer

This will only ever run once.

local lightPart = game.Workspace.LightPart
game.Lighting.LightingChanged:connect(function()
	local timeAfterMidnight = game.Lighting:GetMinutesAfterMidnight()	
	if timeAfterMidnight >= 8 * 60 and timeAfterMidnight <= 18 * 60 then
	        lightPart.Material = Enum.Material.Plastic
	        lightPart.PointLight.Enabled = false
	elseif timeAfterMidnight >= 18* 60 and timeAfterMidnight <= 8 * 60 then
	        lightPart.Material = Enum.Material.Neon
	        lightPart.PointLight.Enabled = true
	end
end)

You need to use a loop to run the function constantly.

For multiple parts

Scenario I have a folder in workspace called "Lights". It has many parts in it.

local lightStorage = game.Workspace.Lights:GetChildren()
game.Lighting.LightingChanged:connect(function()
	local timeAfterMidnight = game.Lighting:GetMinutesAfterMidnight()
	if timeAfterMidnight >= 8 * 60 and timeAfterMidnight <= 18 * 60 then
		for i,v in pairs(lightStorage) do
	        	v.Material = Enum.Material.Plastic
	       	 	v.PointLight.Enabled = false
		end
	elseif timeAfterMidnight >= 18* 60 and timeAfterMidnight <= 8 * 60 then
		for i,v in pairs(lightStorage) do
	        	v.Material = Enum.Material.Neon
	       	 	v.PointLight.Enabled = true
		end
	end
end)

5 comments

0

Thanks a bunch! Should've known I needed to use a loop oops.

0

I changed the script around, but it only turns off one light out of the many. Now I'm really confused lol

0

This specific solution wouldn't work as it only checks the time once

0

I tried your solution as well IDidMakeThat and it has the same effect, only one of the LightParts gets disabled D:

0

The lights all do in fact turn on during the day time, but once it's night time, the lights don't turn on. Sorry I'm a noob, but I just started out! xD

The problem is likely that you have not put it in a loop. For this, you should use a while loop.

local lightPart = game.Workspace.LightPart
while true do
	if game.Lighting:GetMinutesAfterMidnight() >= 8 * 60 then
	        lightPart.Material = Enum.Material.Plastic
	        lightPart.PointLight.Enabled = false
	    end
	if game.Lighting:GetMinutesAfterMidnight() >= 18 * 60 then
	        lightPart.Material = Enum.Material.Neon
	        lightPart.PointLight.Enabled = true
	    end
	wait()
end

The reason I use the >= operator is that the script may miss the point where it is exactly that time.

2 comments

0

It would be more efficient if you used an elseif instead of two if statements.

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.