Function causing massive lag

I made a function that disables/enables certain bright aspects of my game to make it easier on the eyes but whenever it runs it causes massive lag and causes ping to go extremely high.

function graphics()

	if graphicsDB == false then
		graphicsDB = true

		

		for i,v in pairs(workspace:GetDescendants()) do
			if v:IsA("BillboardGui") and v.Name == "LightGlow" or v:IsA("Beam") and v.Name == "LightBeam" then
				v.Enabled = false
			end

			if v:IsA("BasePart") and v.Reflectance > 0 then
				v:SetAttribute("OldReflectance", v.Reflectance)
				v.Reflectance = 0
			end

			if v:IsA("BasePart") and v.Material == Enum.Material.Neon then
				v.Material = Enum.Material.SmoothPlastic
				if v:GetAttribute("WasNeon") == nil then
					v:SetAttribute("WasNeon", true)
				end
			end
		end
		
		script.Parent.DBrightEffects.Text = "Show bright effects"
		game.ReplicatedStorage.UpdateSetting:FireServer("HideBrightEffects", true)
	else
		graphicsDB = false

		for i,v in pairs(workspace:GetDescendants()) do
			if v:IsA("BillboardGui") and v.Name == "LightGlow" or v:IsA("Beam") and v.Name == "LightBeam" then
				v.Enabled = true
			end

			if v:IsA("BasePart") and v:GetAttribute("WasNeon") then
				v.Material = Enum.Material.Neon

			end

			if v:IsA("BasePart") and v:GetAttribute("OldReflectance") then
				v.Reflectance = v:GetAttribute("OldReflectance")
			end
			script.Parent.DBrightEffects.Text = "Hide bright effects"
			game.ReplicatedStorage.UpdateSetting:FireServer("HideBrightEffects", false)
		end
	end

end

I need help with optimizing this function if possible.

1 Like

How is this function executed? A RunService connection? A button press?

2 Likes

GUI button press
It runs on the client

1 Like

Should of specified that, sorry

1 Like

Couldn’t you just use CollectionService and tag all the items that are gonna be changed?

2 Likes

Looks like you are doing something on the server. Have you checked if the lag is applied to the entire server, or just the client?

1 Like

I forgot about CollectionService
That would probably work better

1 Like

Well when it lags out your chat messages are super delayed
I don’t know if that’s from client or server lag though

Here’s a command you can run that will apply tags appropriately (run it through the Command Bar):

local collectionService = game:GetService("CollectionService") for _, instance in ipairs(game:GetDescendants()) do if instance:IsA("BillboardGui") and instance.Name == "LightGlow" or instance:IsA("Beam") and instance.Name == "LightBeam" then collectionService:AddTag(instance, "LightBoardBeam") end if instance:IsA("BasePart") and instance:GetAttribute("WasNeon") then collectionService:AddTag(instance, "Neon") end if instance:IsA("BasePart") and instance:GetAttribute("OldReflectance") then collectionService:AddTag(instance, "Reflectance") end end

Edit: Just noticed I was getting if statements from the bottom half of the graphics function, shouldn’t do anything bad to your game but it won’t fully work. Let me rewrite real quick.

Edit 2:

local collectionService = game:GetService("CollectionService") for _, instance in ipairs(game:GetDescendants()) do if instance:IsA("BillboardGui") and instance.Name == "LightGlow" or instance:IsA("Beam") and instance.Name == "LightBeam" then collectionService:AddTag(instance, "LightBoardBeam")end if instance:IsA("BasePart") and instance.Reflectance > 0 then collectionService:AddTag(instance, "Reflectance") end if instance:IsA("BasePart") and instance.Material == Enum.Material.Neon then collectionService:AddTag(instance, "Neon") end end
2 Likes

workspace:GetDescendants() returns a MASSIVE table containing every single object in workspace, your performance issue is with sorting through all the data the logic (on robloxes end) retrieving data

Try switching it out for Collection service (like others have said) since you can just tag parts that need to be changed, either that or place your effects in a folder rather than workspace letting you sort through like 40 objects instead of thousands+

Large tables are a pretty big performance draw, we have to be cautious when using tables in TPGI for example since they can quickly build lag and performance issues.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.