How to make a light system using a single script

I have been trying for the past couple of hours to make a lighting system for my games streetlights so they turn on automatically.

But the current system i have requires you to have a script in each part/union which causes a lot of lag. So i’ve been trying to make it so it’s all controlled by a single script but have failed to do so.

Appreciate any help on this subject!

If needed i can try provide more details.

b = script.Parent

local oh,om = 6,30	-- Daytime
local ch,cm = 17,30	-- Nighttime

local l = game:service("Lighting")
if (om == nil) then om = 0 end
if (cm == nil) then cm = 0 end


function TimeChanged()
	local ot = (oh + (om/60)) * 60
	local ct = (ch + (cm/60)) * 60
	if (ot < ct) then
		if (l:GetMinutesAfterMidnight() >= ot) and (l:GetMinutesAfterMidnight() <= ct) then
			b.Material = "SmoothPlastic"
			b.Reflectance = "0.45"
			b.Color = Color3.new(0.47451, 0.482353, 0.513725)
			b.PointLight.Enabled = false
		else
			b.Material = "Neon"
			b.Reflectance = "0"
			b.Color = Color3.new(0.639216, 0.576471, 0.454902)
			b.PointLight.Enabled = true
		end
	elseif (ot > ct) then
		if (l:GetMinutesAfterMidnight() >= ot) or (l:GetMinutesAfterMidnight() <= ct) then
b.Transparency = 0
		else
b.Transparency = 0
		end
	end
end

TimeChanged()
game.Lighting.Changed:connect(function(property)
			if (property == "TimeOfDay") then
				TimeChanged()
			end
		end)
2 Likes

Hi, what you could do is you can put all streetlights in a folder, and then 1 script outside of the folder to loop through the children of the folder and make any necessary changes to the streetlight.

1 Like

Hi would you possibly know how i’d do this?

I’m not very familiar when it comes to :GetChildren

In the script you would run:

local folder -- path to folder

for i, v in pairs(folder:GetChildren()) do
    if v:IsA('Model') then -- this is assuming all the street lights are a model
        -- run code right here
    end
end

(this code is untested and may return an error, please reply if it does error)

-Grayseon