Is there such thing as Object:NthChild()?

I scoured some of the Roblox Developer Hub and some of the Object: functions but couldn’t find it. Is there a way to achieve it?

I want for it to work like a table, where you could do table[n] to get the nth child. How would you do this in Workspace?

1 Like

What is this for?

The thing is for nth child Is that usually it’s not reliable to rely on the order of the children.

If it’s about sorting the children named in a specific order part 1, part 2 you can use table.sort.

1 Like

I don’t need it to be reliable, I just need it so I can convert a number to a child of an object.

1 Like
function getNthChild(object, n)
	return object:GetChildren()[n]
end

local fifthChild = getNthChild(workspace, 5) 
print(fifthChild)

PS: :GetChildren() returns an array of all the object children, there’s also :GetDescendants() which has the same behavior but returns all the descendants(all the objects under the object, not just the children).

2 Likes

Thank you! This is all I needed.

3 Likes

Define your functions locally please.

local Folder = workspace.Folder

local FolderInstanceFive = Folder:GetChildren()[5]

To the thread’s poster, as has been stated, “:GetChildren()” returns an array of instances so you can simply index this array with a number in order to retrieve the value (in this case some child instance) which is located/stored at the selected index (represented by the specified number, in the above example that was 5).