You will have to sort the table of children yourself. Luckily for you, lua has a sort function for tables that allow you to use a custom sorting function: table.sort
.
table.sort(t, comp)
The function’s first argument is the table you want to sort, and the second is the comparison function. The comparison function has to have 2 parameters, and the function has to return true if the first parameter is supposed to be before the second.
Here’s an example:
16 | for k, v in pairs (tab) do |
17 | print (v.Name, v.Value) |
25 | table.sort(tab, function (first, second) |
26 | return first.Value < second.Value |
29 | for k, v in pairs (tab) do |
30 | print (v.Name, v.Value) |
We can apply this to your problem. One way is to have a dictionary where the names are associated with an order:
07 | table.sort(tableOfParts, function (first, second) |
09 | return order [ first.Name ] < order [ second.Name ] |
Another way is to add an IntValue in the NPCs, assuming you’re using GetChildren on a model containing all NPCs. Name this IntValue “Order” or however you like, then make it’s value its order. To be clear, you do this in studio while you’re not testing.
1 | local NPCs = workspace.NPCs:GetChildren() |
3 | table.sort(NPCs, function (first, second) |
4 | return first.Order.Value < second.Order.Value |
Hope this helps.