How can I reduce lag, when turning on street lamps?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to reduce lag overall, but one issue that is the most problematic is my street light script.

  2. What is the issue? Include screenshots / videos if possible!

Everytime my streetlight script runs:

local descendants = game.Workspace.StreetLightsCombined:GetDescendants()





local Light = game.ReplicatedStorage.StreetLight

-- SET FOR WHEN FIRST MADE
if  game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 17.7 then
	--print("1124")
	-- off
	for index, descendant in pairs(descendants) do
		--print("1124")
		--print(descendant.Name)
		if descendant.Name == "StreetLightBulbI" then
			print("1124")
			if descendant:IsA("MeshPart")then
				print("1124")
				descendant.Transparency = 0
				descendant.BrickColor = BrickColor.new("Institutional white")
				descendant.Material = "ForceField"
				local steetLightClone = Light:Clone()
				steetLightClone.Parent = descendant
				steetLightClone.Brightness = 0
				print("HAAAA")
			end
		end
	end
elseif  game.Lighting.ClockTime <= 6.5 or game.Lighting.ClockTime >= 17.7 then
	print("1124")
	-- on
	for index, descendant in pairs(descendants) do
		--print("1124")
		if descendant.Name == "StreetLightBulbI" then
		--	print("1124")
			if descendant:IsA("MeshPart")then
				print("1124")
				descendant.Transparency = 0
				descendant.BrickColor = BrickColor.new("Institutional white")
				descendant.Material = "Neon"
				local steetLightClone = Light:Clone()
				steetLightClone.Parent = descendant
				steetLightClone.Brightness = 2.14
				print("AHHHH")
			end
		end
	end
else
	print("HUH")
end
---------------------



local part = game.Lighting

-- Save the current state
local currentTime = part.ClockTime

local function onBrickColorChanged()
	local newTime = part.ClockTime
	--print("Color changed from " .. currentTime .. " to " .. newTime)
	currentTime = newTime
	--print("TIMEEEE")
	print(newTime)
	if newTime < 6.5 and newTime > 6.35 then
		for index, descendant in pairs(descendants) do
			--print("1124")
			--print(descendant.Name)
			if descendant.Name == "StreetLightBulbI" then
				print("1124")
				if descendant:IsA("MeshPart")then
					print("1124")
					descendant.Transparency = 0
					descendant.BrickColor = BrickColor.new("Institutional white")
					descendant.Material = "ForceField"
					descendant.StreetLight.Brightness = 0
					print("HAAAA")
				end
			end
		end
	elseif newTime < 17.7 and newTime > 17.5 then
		for index, descendant in pairs(descendants) do
			--print("1124")
			if descendant.Name == "StreetLightBulbI" then
				--	print("1124")
				if descendant:IsA("MeshPart")then
					print("1124")
					descendant.Transparency = 0
					descendant.BrickColor = BrickColor.new("Institutional white")
					descendant.Material = "Neon"
					descendant.StreetLight.Brightness = 2.14
					print("AHHHH")
				end
			else
				print("Else")
			end
		end
	else
		print("Else")
	end



end 
part:GetPropertyChangedSignal("ClockTime"):Connect(onBrickColorChanged)

it lags a LOT. I know that there’s probably an easier way to do this, and that’s why I’m here for help. I got some help from other Roblox communities but my lag persisted every time the lamps would turn on.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve (as you can see) tried to put the light part into the lamp when the script first runs then I enable and disable them accordingly.

TROUBLE SHOOTING

The huge problem is that YOUR SCRIPT CONSTANTLY RUNS. The clock time constantly changes, therefore part:GetPropertyChangedSignal("ClockTime"):Connect(onBrickColorChanged) is always detecting changes.

Your current logic is this:

--[[
I want to constantly detect if it is day or night.
I'm constantly looping through every single object every time the time of day changes.
]]

Let’s say you have 1000 lights to turn on and off, you are constantly looping through that 1000 lights non-stop, even when the conditions are not true, which is why it is a bad idea.

Your logic has to be:

--[[
I want to constantly detect if it is day or night.
I'm only going to loop through every single object ONLY WHEN IT IS DAY OR NIGHT.
]]

Your possible solution will be:

  1. Make a bool value named IsNight, keep the bool value as False
  2. You want to run a separate function to detect if it is day or night.
  3. If it is night time, flip the bool value to true. Otherwise, if it is day time, flip the bool back to false.
  4. Connect the onBrickColorChanged ONLY WHEN the bool value IsNight is on or off.
3 Likes

There are a few things I see that could contribute to the lag

  1. There are a lot of print statements happening, loads of them can cause lag to happen from my experience
  2. IsA is costier as it checks inheritance as well. In your case, it’s not needed so you can just directly check the classname instead
  3. The code runs by checking if the ClockTIme is at a specific range, it’s much better to check if ClockTime is at a specific number, (if you want the lights to turn on at 17:30, do that only when ClockTime is 17.5 to reduce the amount of times it loops)

I would recommend reducing the amount of prints, using ClassName == "MeshPart" instead of IsA("MeshPart") and check specific ClockTime values instead of ranges if you’re changing the time minute by minute. Here’s how I would do it

Code relating to the event:

local part = game:GetService("Lighting")

-- Save the current state
local currentTime = part.ClockTime

local function onBrickColorChanged()
	local newTime = part.ClockTime
	currentTime = newTime
	print(newTime)
	if newTIme == 6.5 then -- 6:30
		print("Day Time")
		for _, descendant in pairs(descendants) do
			if descendant.Name ~= "StreetLightBulbI" or descendant.ClassName ~= "MeshPart" then continue end
			descendant.Material = Enum.Material.ForceField
			descendant.StreetLight.Brightness = 0
		end
	elseif newTime == 17.5 then --17:30
		print("Night Time")
		for _, descendant in pairs(descendants) do
			if descendant.Name ~= "StreetLightBulbI" or descendant.ClassName ~= "MeshPart" then continue end
			descendant.Material = Enum.Material.Neon
			descendant.StreetLight.Brightness = 2.14
		end
	else
		return
	end
end 
part:GetPropertyChangedSignal("ClockTime"):Connect(onBrickColorChanged)

Hopefully this should reduce the amount of lag to barely any or none. Also in both loops it changes the transparency and brickcolor of them t othe same thing that they were initalized with, so I’d remove them unless it was a mistake and you meant to make the brickcolors different

1 Like