Do you have a table of the items you need in the correct order, for example Pistol at index 1, and so on? If not, you’ll need to sort it.
I’d recommend having a table of the desired item order with number references., where 1 is the top and the largest number is the bottom.
However, if you are using nested tables, for example for each category of weapon, you’ll need a more complex approach.
Here’s a quick example using table.sort
to sort the items based off of priority.
local priorities = { --priorities; the order
["Guns"] = {
["Pistol"] = 1,
["Gun2"] = 2,
["Gun3"] = 3
},
["Planes"] = {
["Plane1"] = 1,
["Plane2"] = 2,
["Plane3"] = 3
}
}
local items = { --the items themselves
["Guns"] = {
"Gun3",
"Gun2",
"Pistol"
},
["Planes"] = {
"Plane3",
"Plane1",
"Plane2"
}
}
for k, v in next, items, nil do
table.sort(v, function(a, b)
return priorities[k][a] < priorities[k][b]
end)
end
table.sort
in Luau uses a variation of the Quicksort algorithm, which is pretty efficient at sorting items. However, for more nested data structures it may eventually become more efficient to look into other sorting algorithms like a Merge sort for example, which is inherently recursive and may be more efficient if you have more nested data structures, and could be good after a bit of modification.
Any questions, please ask.