How does Roblox read the children of a model when using :GetChildren()?

When a script uses GetChildren(), will the script read the model’s children from top to bottom (alphabetically) as seen in the Explorer tab or something else?

1 Like

For example, if I had a model with 4 part of the same name.
26%20PM

Will the script always assign the top block as i = 1? Also, if I were to save and reload my game, will the script turn in a different answer?

1 Like

The children are returned in ChildOrder, this is the order that the children are added in. The first children to be added are first in the table GetChildren returns.

This is however not guaranteed and it could be changed in the future.

6 Likes

Thanks for the answer! I won’t be needing it for more than a few minutes though so it shouldn’t be a problem as of right now. Thanks again.

Out of curiosity, is there a specific reason that you need to know the order for which children are returned in? You can typically use Instance::GetChildren and work based off of the return values without needing to know their exact order.

If you want an alphabetical version A-to-Z:

function GetChildrenAlphabetical(instance)
	local children = instance:GetChildren()
	table.sort(children, function(c1, c2)
		return c1.Name:lower() < c2.Name:lower()
	end)
	return children
end

--

local children = GetChildrenAlphabetical(someModel)
6 Likes

Bear in mind that this will not take into account that the ascii values of capitals are lower, so you’d have to use string.lower() on both names for a truly alphabetical result.

1 Like