Streetlight Script

I was wondering if my scripts are efficient, but I’m also thinking that it could be improved for minimal lag.

This is the script in ReplicatedStorage

module.mod = function()
	local DT = game.Lighting.ClockTime
	if DT >= 6.00 and DT < 17.00 then
		return "NO"
	elseif DT >= 17.00 or DT < 6.00 then
		return "YES"
	else
		return "MAYBE"
	end
end
return module

This is the script that is in all the streetlights

local LightBulb = script.Parent.Parent.GlassShell
local data = require(game.ReplicatedStorage.CheckModule)
local ParticleEmitter = script.Parent.Parent.Head.Particle.ParticleEmitter

while true do
	local result = data.mod()
	if result == "YES" then
		script.Parent.Material = Enum.Material.Neon
		script.Parent.SpotLight.Enabled = true
		script.Parent.Parent.Beam.Beam.Beam.Enabled = true
		LightBulb.Transparency = 1
	else
		script.Parent.Material = Enum.Material.SmoothPlastic
		script.Parent.SpotLight.Enabled = false
		script.Parent.Parent.Beam.Beam.Beam.Enabled = false
		LightBulb.Transparency = 0.1
		
	end
	wait(30)
end

It already lags at night, but I’m thinking it is just how many streetlights are on. I’m not quite sure if this is the best way to turn on the streetlights because I don’t feel like I should have a script for every streetlight. I was wondering if I need to fix this and how.

One thing that comes to my mind when dealing with this type of thing is for example with my proximity prompts what I do is put them all inside of a folder and everytime the render is stepped I go through the folder and check the distance from each part inside of the folder. Maybe you can do something similar with the streetlights like for example inside of a script somewhere you check for when the result is ‘YES’ then do a loop.

while true do
      local result = data.mod()
      if result == 'YES' then

            for i, v in pairs(workspace.**YOUR FOLDER**:GetChildren()) do
                v.Material = Enum.Material.Neon
		        v.SpotLight.Enabled = true
		        v.Parent.Beam.Beam.Beam.Enabled = true
		        v.Parent.GlassShell.Transparency = 1
            end

      else

           for i, v in pairs(workspace.**YOUR FOLDER**:GetChildren()) do
                v.Material = Enum.Material.SmoothPlastic
		        v.SpotLight.Enabled = false
		        v.Parent.Beam.Beam.Beam.Enabled = false
		        v.Parent.GlassShell.Transparency = 0.1
            end
      end
end
1 Like

I did that, it works perfectly. Thank you!

1 Like

No issue, if you have anything else that you need to follow up on let me know.

1 Like