How to add objects into arrays in numerical order?

I’m trying to add these objects into an array in numerical order, but it behaves differently.

Objects:

  • Box_01
  • Box_02
  • Box_03
  • Box_04

However, it adds the objects like this:

  • Box_02
  • Box_01
  • Box_04
  • Box_03

Here’s my code:

   local boxes = {};

   for i, v in ipairs(workspace.Objects.Boxes:GetChildren()) do
       table.insert(boxes, v);
       print(v);
   end
1 Like

The issue is when you :GetChildren() and not the table.insert. It doesn’t promise you order. I’d give each one an attribute with the order (rather than name them, but you can extract the number from the name as well though it means going through the whole string each time).

for _,v in pairs(the data table) do
   table.insert(data table,  v:GetAttribute("Order"), v)
end
2 Likes