I am trying to make a game with a lot of saveable items. Problem is, when saving and loading these items they are ordered based on how recently they were unequipped.
I’m wondering if there is any method at all to read what order these tools are actually presented in the player’s backpack so they can be loaded back in the same order.
You can use custom inventory system and using LayoutOrder for that.
Also you can save instances as array and they will always be loaded in a same order if done right.
Just make sure to NOT use iterator functions like pairs/ipairs/next and iterate over table dirrectly instead.
Roblox Tools seem to be ordered based off of how recently one was added to the Backpack, with the first added being the first tool in your hands, so, something like
local Tools = {}
for i, v in ipairs(Backpack:GetChildren()) do
table.insert(Tools, v)
v.Parent = nil
end
table.sort(Tools, function(a, b)
return a or b --Reorder the table here with your sorting logic
end)
for i, v in ipairs(Tools) do
v.Parent = Backpack
end
table.clear(Tools)
You can’t do this with the default backpack, the order of the tools can be altered by players, and the backpack doesn’t offer a way to get the order of the tools
You can look into these custom backpacks
Most of these seem to be built upon the default backpack (it’s a lua corescript you can find with some digging), however, all of them except InventoryMaker don’t seem to provide an api method to get the order of the tools in the inventory. You could probably get the table of tools (if that’s how they are setup) by modifying the source code of these backpack modules
ipairs is meant to iterate over arrays in order, and is totally appropriate for this use case