How to reduce lag on looping through 500+ children in a folder

so the issue is that i have a folder that has all the walls the player has placed, because i have a wall placement system, and the issue with it is that when the player recolors a wall the only way to find that wall (at least in my system) is to loop through ALL the children inside the wall folder, and when it finds the wall it recolors it, and it would reset all the other walls to the default color which is just gray.

and this is just a small example i have a lot of other systems that require looping through 500+ children in a folder, and i dont know how to fix the lag, because i have a decent computer and it might not lag as much as people who are using ipads, so i cant really test it that perfectly so even though it might be not that laggy for me (even though it is) it would be laggy for people playing in their ipads

any help will be appreciated!

heres the code:

		local function Recolor(Folder, OtherFolder)
			
			for i,RepWall in Folder:GetChildren() do  -- this folder is the source folder so its the folder that the other folder will take the colors from
				for i,Wall in OtherFolder:GetChildren() do -- this folder is the wall folder so it may has 500+ walls (including walls that the player cant recolor)
					if RepWall:IsA('BasePart') and Wall:IsA('BasePart') then
						if RepWall:GetAttribute("SeralizedName") and Wall:GetAttribute("SeralizedName") and RepWall:GetAttribute("SeralizedName") == Wall:GetAttribute("SeralizedName") then
							Wall.Color = RepWall.Color
						end
					elseif RepWall:IsA('Model') and Wall:IsA('Model') then
						if RepWall.PrimaryPart:GetAttribute("SeralizedName") and Wall.PrimaryPart:GetAttribute("SeralizedName") and RepWall.PrimaryPart:GetAttribute("SeralizedName") == Wall.PrimaryPart:GetAttribute("SeralizedName") then
							Wall.PrimaryPart.Color = RepWall.PrimaryPart.Color

							if Wall:FindFirstChild("BackFace") and Wall:FindFirstChild("FrontFace") then
								Wall:FindFirstChild("BackFace").Color = RepWall.PrimaryPart.Color
								Wall:FindFirstChild("FrontFace").Color = RepWall.PrimaryPart.Color
							end	

						end
					end
				end
				
			end
			
			for i,RepWall in Folder:GetChildren() do
				for i,Wall in script.Parent.WorldModel:GetChildren() do
					if RepWall:IsA('BasePart') and Wall:IsA('BasePart') then
						if RepWall:GetAttribute("SeralizedName") and Wall:GetAttribute("SeralizedName") and RepWall:GetAttribute("SeralizedName") == Wall:GetAttribute("SeralizedName") then
							Wall.Color = RepWall.Color
						end
					elseif RepWall:IsA('Model') and Wall:IsA('Model') then
						if RepWall:GetAttribute("SeralizedName") and Wall:GetAttribute("SeralizedName") and RepWall.PrimaryPart:GetAttribute("SeralizedName") == Wall.PrimaryPart:GetAttribute("SeralizedName") then
							Wall.PrimaryPart.Color = RepWall.PrimaryPart.Color
							Wall:FindFirstChild("BackFace").Color = RepWall.PrimaryPart.Color
							Wall:FindFirstChild("FrontFace").Color = RepWall.PrimaryPart.Color
						end
					end
				end
			end
			
		end
		
		Recolor(ReplicatedStorageInterior:FindFirstChild("Walls"), Interior:FindFirstChild("Walls"))

I’m on mobile in the car so I won’t be able to fully assist with the issue since the code and logic is quite a mess. However, I did want to give a tip that should improve it somewhat. Using FindFirstChild should only be done to check if a certain child exists. If you are 100% sure if exists, just index it directly using the “.” operator. If you know it exists which you are checking in your if statement, there is no need to index it using FindFirstChild after that, as it’s generally slower than indexing it directly.

if Wall:FindFirstChild("BackFace") and Wall:FindFirstChild("FrontFace") then
								Wall:FindFirstChild("BackFace").Color = RepWall.PrimaryPart.Color
								Wall:FindFirstChild("FrontFace").Color = RepWall.PrimaryPart.Color
							end	

Note this doesn’t fix the underlying problem. There is no need to loop through all walls if you want to recolor one. Just recolor the wall that needs to be recolored.

1 Like

I don’t quite understand what you’re doing. If the player chooses a wall to recolor, why do you have to loop through every wall? Can’t you just color the one they chose when they choose it?

1 Like

so i apolgise for the confusion, i just was able to fix the issue for the wall looping thing, and there isnt any lag now, but im still having an issue with looping through other folders that have a lot of children inside it.

if i casually use for i,v in Folder:GetChildren() do it would lag because suppose i want to recolor every single child inside that folder that has 500+ children it would lag everyone because the recoloring is done on the server

Why do you need to recolor every wall in the folder? Just store a reference to the walls that have a different color and color those to the default color when needed.

1 Like

Going through 500 children should be nothing. If you’re using FindFirstChild however, then I’d heavily recommend you to just use the ‘.’ to directly get the child like the other guy said.

1 Like

yes thats what i did and now it has no issue with the lag but the original problem still there, the fact that looping through a lot of children it would lag

but whats confusing me right now that if there isnt any issue with recoloring children’s folders that have a lot of children inside it then why is it lagging when i recolor all walls?

I still don’t understand why you need to loop through the entire folder. From my understanding you want to color a single wall and set every other wall to the default gray color. Correct me if I’m mistaken.

I’d assume it’s because of the :FindFirstChild. It basically goes through every child of the folder until it finds the one with the given name which in your case would be at worst 500*500 = 250000 children being checked. Although roblox has most likely got some optimizing going on with the function, then it’s still not very fast.

1 Like

so just to clear all confusing my system is as follows:

i let the player choose a wall to recolor in the client and he picks the color,

then he sends that new color AND the seralized name for the wall he recolored

and then the server loops through all the children inside the wall loop and it checks for all the wall’s seralized names and if it finds a match between the sent seralized name and the current seralized name then it recolors it in the server.

the issue here is that if the player placed 600 walls, the server would need to loop through EVERY wall in that folder to check if the seralized name matches, but that would cause lag

which is the current lag, i did fix the client lag by just recoloring the selected wall only and not every single wall,

but the server thing seralized name matching is still an issue

hope thats clear if you want me to clarify even further then tell me

His way of going through the entire folder to change a property of a single wall is still extremely inefficient regardless of using FindFirstChild or not. I’d like to know why it’s not possible to directly adjust the properties of the selected wall.

Why do you send a serialized name and not just the wall instance that needs to be recolored?

You can’t send instances like that through remote events.

because the wall the client recolored is client sided and the server cant see it, so i cant send it directly

Did you try removing the FindFirstChilds tho?

1 Like

500 children really shouldn’t be that much by itself.

1 Like

so i should just make it wall.BackFace.Color and wall.FrontFace.Color

1 Like

Yeah. As was mentioned before, if you’re sure the object exists, you shouldn’t use findfirstchild

1 Like

so the server looping through the folder and checking the seralized name shouldnt be an issue right?