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

How to turn on multiple lights with one script when evening hits?

Hi,

I have a problem with my Streetlight-day/night cycle script.

I want to turn on multiple streetlights at once when evening/18:00:00 hits. The script works just fine with one streetlights but with 2 or more not.

see script below or link to screenshot

The problem is that the for loop checks the location, sees a PointLight, returns, and do it again and again. But the script completely skips the other streetlights because it already found the PointLight in the first streetlight that was needed (correct me if im wrong).

This is the code were Im struggling with:

local lightName = "TL" --Name of parts to store TL in
local spotLightName = "SpotLight" --Name of point TL or other TL object with .Enabled property
local model = game.Workspace.Lights.StreetLights.StreetLight.Light

while wait() do
	if game.Lighting.TimeOfDay >= "18:00:00" then
		for i, TL in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if TL.Name==lightName then --It is a TL and not just another part
				TL.Material = Enum.Material.Neon
				TL.Transparency = 0
				TL[spotLightName].Enabled = true --Disable TL
				end
			end
	elseif game.Lighting.TimeOfDay >= "06:00:00" then
		for i, TL in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if TL.Name==lightName then --It is a TL and not just another part
				TL.Material = Enum.Material.Glass
				TL.Transparency = 0.25
				TL[spotLightName].Enabled = false --Enable TL
			end
		end
	end
	
	wait()
end

https://imgur.com/a/KwiZQSo

I hope someone can help me.

If you can help me please keep it easy im a beginner.

Sorry if my grammar is terrible. (main language is Dutch)

1 answer

✓ Accepted answer

try this. if it doesn't work, let me know what happens. i can't test this so i won't know until i get back from you.

local Lights = workspace:WaitForChild('Lights')
local StreetLights = Lights:WaitForChild('StreetLights')
local Lighting = game:GetService('Lighting')

function SetLights(bool)
	if bool == "on" then
		for i,v in pairs(StreetLights:GetDescendants()) do
			if v:IsA('SpotLight') and v.Parent:IsA('BasePart') then
				v.Enabled = true
				v.Parent.Material = Enum.Material.Neon
				v.Parent.Transparency = 0
			end
		end
	elseif bool == "off" then
		for i,v in pairs(StreetLights:GetDescendants()) do
			if v:IsA('SpotLight') and v.Parent:IsA('BasePart') then
				v.Enabled = false
				v.Parent.Material = Enum.Material.Glass
				v.Parent.Transparency = .25
			end
		end
	end
end

local Day = true
while wait() do
	if Lighting.TimeOfDay >= "18:00:00" and Day then
		Day = false
		SetLights("on")
		
	elseif Lighting.TimeOfDay >= "06:00:00" and not Day then
		Day = true
		SetLights("off")
		
	end
end

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