Automatic on / off street light script

Hi all! Thank you for the support on my last post, it’s greatly appreciated.

I’ve been messing around in Roblox, trying to get this script to work, but I’m missing something and I can’t find the answers elsewhere.

I’m trying to write a script that allows either

A.) Local scripts (ie one script per lightpost) that automatically turn a light on / off depending on the clock time (8-16 or so-off, all other times on)

B.) The same concept, however put into ServerScriptService to allow for one script that is compatible with existing light fixtures

I’m stumped on where to go. I’m very new to scripting, so apologies if this is an obvious error.

Thanks for reading!
~Ultra

A few issues.

Your variable needs to have a name, and LightPart isn’t defined. Ex: local LightPart = game.Workspace.LightPart

Another thing, ClockTime comes from the lighting, so do game.Lighting.ClockTime

Here’s an example of how you could do this

--- Very simple
local LightPart = -- path

while task.wait(1) do
     if game.Lighting.ClockTime < 16 and game.Lighting.ClockTime > 8 then
       if not LightPart.PointLight.Enabled then
          LightPart.PointLight.Enabled = true
       end
     elseif LightPart.PointLight.Enabled == true then
        LightPart.PointLight.Enabled  = false
    end
end

If you want do do it for lets say all lights you could do:

local LightParts = -- path to lights

	game.Lighting.LightingChanged:Connect(function()
		for _, LightPart in ipairs(game.PathToLights:GetDescendants()) do
			if LightPart:IsA("PointLight") or LightPart:IsA("SpotLight") or LightPart:IsA("SurfaceLight") then
				
				if game.Lighting.ClockTime < 16 and game.Lighting.ClockTime > 8 then
					if not LightPart.PointLight.Enabled then
						LightPart.PointLight.Enabled = true
					end
					
				elseif LightPart.PointLight.Enabled == true then
					LightPart.PointLight.Enabled = false
				end
			end
		end
	end)

It’d probably be better to do B imo. Put all of your light models into a folder named Lightposts.

Here’s the code you would do:

local lightposts = workspace.Lightposts

while task.wait() do
	for _, lightpost in pairs(lightposts:GetChildren()) do
		if game.Lighting.ClockTime < 16 and game.Lighting.ClockTime > 8 then
			if lightpost.Part.LightPart.Enabled then
				lightpost.Part.LightPart.Enabled = false
			end
		elseif not lightpost.Part.LightPart.Enabled then
			lightpost.Part.LightPart.Enabled = true
		end
	end
end

PS: I was late to put this in, so I used some of the other guy’s code, but it should work inside of a single script.

Issue with doing that is it’s not going to be very performant having 2 loops in a loop, whereas you can avoid the timeout with LightingChanged, plus you’re using the wrong iterator technically ipairs is the correct iterator for :GetChildren and :GetDescendants since it’s meant for arrays.

Another thing, using GetDescendants is more optimal because you can just detect every part’s instance type like pointlight etc, and then turn it off. It would help avoid hierarchy issues. But yeah that would work.

That’s true, I just wanted to put something out there. Sometimes writing things quick isn’t my specialty.

Yeah, no worries. I was just trying to let OP know that there’s more of a benefit to using:

There are plenty of posts about this here, you probably just need to change your search terminology.
For example this post was started by one member, but was improved upon by another member:
Simple Street Light Script! [Beginners Tutorial] - #5 by R41NB0W4C3

What u need for my post:

What u could try using is CollectionService, in cases that a normal for loop doesn’t work, collectionService will solve that issue, which in this case helped me greatly.

I will share my code so u will understand what to do.

local Lighting = game:GetService("Lighting")
local CollectionService = game:GetService("CollectionService")

for _, Lights in CollectionService:GetTagged("Lights") do
	local PointLight = Lights:FindFirstChildWhichIsA("PointLight")
	
	Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
		if Lighting:GetMinutesAfterMidnight() > 6*60 then
			Lights.Material = Enum.Material.Plastic
			PointLight.Enabled = false
		end
		if Lighting:GetMinutesAfterMidnight() > 18*60 then
			Lights.Material = Enum.Material.Neon
			Lights.Transparency = .2
			PointLight.Enabled = true
		end
	end)
end

U can always consider adding TweenService for a smooth transition between enabled/disabling the street light posts.