"in pairs()" Loop question!

Hi. I was wondering how the “for” loop cycles through the children of a part. How does it know which one to print first. Is there a simple way to make it always print Node1, Node2, Node3…

local Model = workspace.Model

for Index,Waypoint in pairs(Model:GetChildren()) do
	print(Waypoint)
end

image

1 Like

Roblox always uses standard alphanumeric order. So Node1 should always appear in a model before Node2.

For iterating over arrays in order, you want ipairs, not pairs.

As for the order of GetChildren, I don’t know how Roblox specifies it, so it might be better to just use a numeric for.

for i = 1, #Model:GetChildren() do
    local Waypoint = Model['Node' .. i]
end

This should work, assuming all your children follow the same naming convention.

3 Likes