GetChildren (Method): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>NXTBoy
Obviously examples can't use meaningful variable names...
Fixing page
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<onlyinclude>{{Method
<onlyinclude>{{Method
|name                = GetChildren
  | name                = GetChildren
|arguments            =
  | returns              = {{type|table}}
|returns              = [[Table]] ''children''
  | description          = Returns a read-only {{type|table}} of the object's children.
|description          = Returns a read-only table of this 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 index, object in ipairs(children) do
     print("index: ",index)
     print("Object:",object)
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