GetChildren (Method): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Camoy
fixed
Fixing page
 
(18 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<onlyinclude>{{Method|name = GetChildren
<onlyinclude>{{Method
|arguments =  
  | name                 = GetChildren
|returns = [[Table]] ''children''
  | returns              = {{type|table}}
|description = Returns a read-only table of this Object's children.
  | description         = Returns a read-only {{type|table}} of the object's children.
|object = [[Instance]]
  | object               = Instance
}}</onlyinclude>
}}</onlyinclude>
{{clear floats}}
{{clear floats}}
*[[Loops#The_for_loop|For loop]] (Simple example)
{{Example|Using GetChildren with a [[Loops#The_for_loop|for loop]].<syntaxhighlight lang="lua">
{{Example|<pre>
local children = Workspace:GetChildren()
 
for i = 1, #children do
local children = game.Workspace:GetChildren()
     print(children[i].Name)
for c = 1, #children do
     print(children[c].Name)
end
end
 
</syntaxhighlight>}}
</pre>}}
{{Example|It is highly recommended to use GetChildren with a [[Generic for|generic for loop]].<syntaxhighlight lang="lua">
*[[Generic for]] (More efficient example)
for index, child in pairs(Workspace:GetChildren()) do
{{Example|<pre>
     print("Index: ", index)
local children = game.Workspace:GetChildren()
     print("Child: ", child)
for i, j in ipairs(children) do
     print("index: ")  --j is the object, i is the index of the table it is at
    print(i)
     print("Object: ")
    print(j)
end
end
</pre>}}
</syntaxhighlight>}}
[[Category:Methods]]

Latest revision as of 00:10, 18 April 2023

GetChildren( )
Returns table
Description: Returns a read-only table of the object's children.
Member of: Instance
Example
Using GetChildren with a for loop.
local children = Workspace:GetChildren()
for i = 1, #children do
    print(children[i].Name)
end
Example
It is highly recommended to use GetChildren with a generic for loop.
for index, child in pairs(Workspace:GetChildren()) do
    print("Index: ", index)
    print("Child: ", child)
end