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.
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
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.