Trying to put two folder:GetChildren() in an in pairs loop

Hey devs!

I’m stuck on a little problem.
I’m trying to make a loop for two or more folder:GetChildren().

So I tried to add a coma like this

for _,	v in pairs(workspace["Map1 / Spawn"].PilesOfLeaves:GetChildren(),workspace.Map2.PilesOfLeaves:GetChildren()) do

A + :

for _,	v in pairs(workspace["Map1 / Spawn"].PilesOfLeaves:GetChildren() + workspace.Map2.PilesOfLeaves:GetChildren()) do

and a “And” :

for _,	v in pairs(workspace["Map1 / Spawn"].PilesOfLeaves:GetChildren() and workspace.Map2.PilesOfLeaves:GetChildren()) do

But it doesn’t work :confused:.

How can I fix it.

2 Likes

Better to use GetDescendants() on a folder with both of the said folders inside and check if the Descendant is the desired Part.

I’d merge them via table.insert, something like this:

local t = workspace["Map1 / Spawn"].PilesOfLeaves:GetChildren();
for _, v in ipairs(workspace.Map2.PilesOfLeaves:GetChildren()) do
    table.insert(t, v);
end;

then ipairs that table

for _, v in ipairs(t) do

end;

+ could work on Python but it doesn’t work on Lua as you’re adding a table value and tables are dynamic in Lua.

and doesn’t work as it gets the first value if it’s falsy (0, 0n, false, undefined, null, etc in Javascript, false and nil in Lua) and the second value otherwise.

Edit: For future users reading, you can also use table.move for this

local t = workspace["Map1 / Spawn"].PilesOfLeaves:GetChildren();
local t2 = workspace.Map2.PilesOfLeaves:GetChildren();
table.move(t2, 1, #t2, #t + 1, t)
for _, v in ipairs(t) do

end
3 Likes

Can you explain me what is the difference between table in pairs and table in ipairs?

pairs goes through all that is in the folder like normal, ipairs is the same but it will stop if it receives a nil value.

Not exactly correct, any non-integral properties are ignored in ipairs as opposed to pairs.
Also ipairs is faster than pairs, apparently (See Luau optimisations).

Not just ignored, but it will stop at missing indices (nil values). GetChildren can always be safely used with ipairs but it should be noted that once you have tables with nil values you need to take care.

Oh ok thx.

I have some random questions to ask, does anyone of you can add me on discord for help me pls? :smiley:

It now works, I changed my script to:

local piles = {}
		for _, v in pairs(workspace["Map1 / Spawn"].PilesOfLeaves:GetChildren()) do
			if v and v:IsA("MeshPart") then
				table.insert(piles, v)
			end
		end
		for _, v in pairs(workspace.Map2.PilesOfLeaves:GetChildren()) do
			if v and v:IsA("MeshPart") then
				table.insert(piles, v)
			end
		end
		for _,	v in pairs(piles) do
2 Likes

I think I have some much things to learn because I’m able to script whatever I want to but it is not optimised…
Thx you for your help.

You can add me on Discord if you want to help me more !
Padrox#8546

I mean ipairs ignores any non-integer key of a table as opposed to pairs and next. ipairs is not the same as pairs.

print('--ipairs--');

for k, v in ipairs{1, 2, [2.5] = 3, [false] = 4, 5} do
	print(k, v)
end;

print('--pairs--');

for k, v in pairs{1, 2, [2.5] = 3, [false] = 4, 5} do
	print(k, v)
end;

prints

--ipairs--
1	1
2	2
3	5
--pairs--
1	1
2	2
3	5
2.5	3
false	4

Also (only for Lua ≥5.2 which doesn’t apply to Roblox which uses Lua 5.1), pairs respects the __pairs metamethod as opposed to ipairs

local example0 = { 1, 2, 3 };
local example1 = setmetatable({ 4, 5, 6 }, { __pairs = function() return pairs(example0); end; });
print('--pairs--');
for k, v in pairs(example1) do
    print(k, v)
end;
print('--ipairs--');
for k, v in ipairs(example1) do
    print(k, v)
end;

prints

--pairs--
1	1
2	2
3	3
--ipairs--
1	4
2	5
3	6

A messy yet simple solution could be something along the lines of this:

local function combineArrays(t1, t2)
    return table.move(t2, 1, #t2, #t1 + 1, t1)
end

local t1 = {1,2,3}
local t2 = {4,5,6}

for i,v in next, combineArrays(t1, t2) do 
	print(i,v)
end

Could also go for a Pythonic approach and implement a zip-like iterator:

local function zip(...)
	local args, args_values = table.pack(...), { }
	local i = 0
	
	return function()
		i = i + 1
		
		for pos, t in ipairs(args) do
			args_values[pos] = t[i]
			
			if args_values[pos] == nil then
				return nil
			end				
		end
		return table.unpack(args_values)
	end
end

for v1, v2 in zip({ "a", "b", "c" }, { "d", "e", "f" }) do
    print(v1, v2)
end

-- a d
-- b e
-- c f

This function can take an arbitrary amount of tables as well.

2 Likes

@Blockzez method will work fine but its over-complicating the issue, and its much simpler and readable if your just merge both folders into a array then iterate over it like this

local Folders = {workspace["Map1 / Spawn"].PilesOfLeaves, workspace.Map2.PilesOfLeaves}
for _, Folder in ipairs(Folders) do
    for _, Child in ipairs(Folder:GetChildren()) do
         --Do whatever with child here
    end
end

2 Likes

Thanks for posting the result of your findings, it was very useful! :+1: