How do I loop through more than 1 thing?

How can I loop through more than one group of instances?
For example how can I merge:

for i, v in pairs(game.Workspace:GetChildren()) do

end

with

for i, v in pairs(game.ServerScriptService:GetChildren()) do

end

Is there a way you can add a group of instances together without having separate loops?
Thanks :slight_smile:

I doubt it would be very optimal to add all of their children to one table, so you might consider putting just the containers in a table. This reduces the syntax to two loops no matter how many containers you want to iterate through.

local containers = {workspace, game:GetService("ServerScriptService")}

for _, container in ipairs(containers) do
	for _, child in ipairs(container:GetChildren()) do
		...
	end
end

Thought of this, but in my for-loop when iIcall the function :GetFullName() I get the error ā€œattempt to call missing method ā€˜GetFullNameā€™ of tableā€.

1 Like

Can you elaborate on how youā€™re using that method?

Here is the code:

for i, v in pairs(game.ServerScriptService()) do
    textBox.Text = v:GetFullName()
end

for i, v in pairs(game.Workspace:GetDesentants()) do
    textBox.Text = GetFullName()
end

I want to merge them because I have some other code that requires them to be together.

Just figured it out. You need to use table.unpack.

You arenā€™t calling a method of ServerScriptService in the first loop. The method in the second loop should be GetDescendants(), and GetFullName() isnā€™t being called by an object.

table.move() provides a convenient way to combine tables.

local childrenA = workspace:GetChildren()
local childrenB = game:GetService("ServerScriptService"):GetChildren()

-- Add childrenB to the end of childrenA
table.move(childrenB, 1, #childrenB, #childrenA + 1, childrenA)

for _, child in ipairs(childrenA) do
	print(child)
end

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