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