Question about ipairs

I was reading api about “ClassName” and saw the code about how to use below;


for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.ClassName == "Part" then
        print("Found a Part")
        -- will find Parts in model, but NOT TrussParts, WedgeParts, etc
    end
end

my question is what “_” and “child” mean. Also why GetChilderen fuction is in ipairs and what ipairs does to the function.

2 Likes

_ is a placeholder (by convention) variable name, which signals that you are not going to use the variable on anything.

ipairs essentially iterates (loop through) the table starting from 1, increasing by 1, until the n-th element is nil.

1 Like

game.Workspace:GetChildren() will return a table containing every object in game.Workspace.

ipairs(table) will return an iterator that will return an index number (1, 2, 3 etc.) and something from the table (table[1], table[2] etc.)
If you do print(ipairs(game.Workspace:GetChildren())), it will print 3 things: a function, the table you gave it earlier, and 0. That’s a Lua iterator, and it’s complicated, so you don’t need to think about it now.

local f, s, v = ipairs(workspace:GetChildren())

_, child are variable names. _ is used here because it’s not used, and usually coders will name it _ to make that clear. child is named this way because the loop is supposed to set it to one of the children of Workspace.
The for loop sets these variables to the results of f(s, v).

local _, child = f(s, v)
-- also: v = _

You don’t seem to know Lua very well, and I don’t know how to explain more simply :[

This code does the same thing without the ipairs():

local children = game.Workspace:GetChildren()
for _ = 1, #children do
	local child = children[_] -- in this case, the _ could be been named i instead
	if child.ClassName == "Part" then
		print("Found a Part")
		-- will find Parts in model, but NOT TrussParts, WedgeParts, etc
	end
end
1 Like

Yep @Eestlane771 and @Blockzez has explained it pretty well,

Firstly what :GetChildren does, it get’s the children which are instances that have parent = to Workspace. In explorer it would look like this:

image

It would return Camera (Maybe not on server as camera only exists locally), Terrain, SpawnLocation, and Baseplate. The order cannot be relied upon due to Luau internal engines somehow.

Here is my attempt at explaining IPairs

local tableOfChildren = game.Workspace:GetChildren()
--Imagine this is in workspace
--What happens if you print out tableOfChildren
local tableOfChildren = {
	[1] = part1; --Some part1 Instance in workspace
	[2] = part2; 
	[3] = Terrain; 
}

for index, value in ipairs(tableOfChildren) do
end

What the for loop does is iterate starting from the first index, 1 until the end of the table.

First it goes to number 1, and obtains the index the number [1] and the value which is part1.

The index is highlighted as green, and value underlined in red.

This is what is accessed by the loop,

for index, value in ipairs(tableOfChildren) do
--For the first iteration
--index = 1
--value = part1
end

Then it will go on to the next entry,

The index is highlighted as green, and value underlined in red. So index = 2 and value is equal to part2

image

for index, value in ipairs(tableOfChildren) do
--For the second iteration
--index = 2
--value = part2
end

And it will continue until the last entry.

Also be aware that the function :GetChildren() doesn’t have a set order like part1, part2, Terrain.

2 Likes

Thank you! So can “game.Workspace:GetChildren()” in ipairs represent the whole table of child in workspace without having to need variable for the whole table?

Yep, it saves a lot of time and effort.

instead of doing manual indexing, (accessing children)

local workspaceChildren = {
[1] = workspace.Part1
[2] = workspace.Terrain
}

You can just do workspace:GetChildren() and filter it with an if statement.

1 Like

Got it. And thanks all Blockzez and Eestlane771