PcoWSkbVqDnWTu_dm2ix
The Developer Hub is now deprecated and information on this page may no longer be accurate. To see our new and improved documentation, please click here. You can read more about the future of documentation here.
Collapse Sidebar

GetDescendants

The GetDescendants function of an object returns an array that contains all of the descendants of that object. Unlike Instance/GetChildren, which only returns the immediate children of an object, GetDescendants will find every child of the object, every child of those children, and so on.

The arrays returned by GetDescendants are arranged so that parents come earlier than their children. Refer to the following example of a Model in the Workspace:

Workspace Descendants

Inside this model are three parts (C, D, and E) and another model (InnerModel). Inside the inner model are two more parts (A and B). Calling GetDescendants on the first model and printing the contents of the returned array would print the first level of children (InnerModel, C, D, and E) before A and B.

local descendants = game.Workspace.Model:GetDescendants()

-- Loop through all of the descendants of the model and
-- print out their name
for index, descendant in pairs(descendants) do
	print(descendant.Name)
end

-- Prints:
-- C
-- D
-- E
-- InnerModel
-- A
-- B

Returns

Return Type Summary

An array containing the Instance’s descendants.


Code Samples


Instance:GetDescendants

GetDescendants is often used to do something to all the descendants that are a particular type of object. The code in this example uses GetDescendants and /Instance/IsA to find all of the parts in the workspace and turns them green.