How to save inventory order in Roblox Studio

Hello! My name is Kyle, and currently I’m trying to get better at scripting. I ran into a problem which I feel would be a common problem for some really good developers. I amn trying to make a script that will keep your guns in order once you change it. (Basically after you reset your guns would stay the way you organize them) I can’t think of a way for the system to know the previous inventory. Can I get some help please?

If you created an array ordered to the specification of the user’s order you could recreate the gun order based on the array. You could save the array in a server side module and require and loop through it when needed.

Can you explain a little more?

I’m assuming your system is either based on the default inventory or a GUI inventory. Let’s say the player orders the guns a certain way, when the player dies you could add the name of the guns one by one to a table. When the player loads back in you could then add the guns one by one back into the player’s inventory. This should mean that each gun is in the same place. The table would look something like this:

local array = {"Gun_1", "Gun_2", "Gun_3"}

your going to want to use GuiObject.LayoutOrder to order the gui

local inventory = {item1, item2, item3}
local frames = {}

-- create a frame for each item in the inventory
for i, item in ipairs(inventory) do
    local frame = Instance.new("Frame")
    frame.LayoutOrder = i
    frames[item] = frame
end
1 Like

How would I do it? I’m not to sure on how to save the array that I had moments before death, and how would I have the same order? And for the array how would I change the guns mid script?

The Humanoid.Died event should be fine for checking if the player has reset. I’m not sure what kind of inventory system you’re using so I can’t give advice on how you could create the array. Either way it would involve looping through the items and adding their names to a table.

The original poster is likely just using the default inventory system.

I believe the items are ordered in the inventory based on the order they are added into it, but when they’re loaded from e.g. StarterPack, the order isn’t quite at your control. It’s eerily consistent, though. I’ve never thought about it before.

As for the problem, my suggestion is to disable item loss on death (if possible). There might be such an option in StarterPack.
If the player isn’t losing or gaining items, then they will simply stay in the order they were in before.
You will have to manually remove items the player should lose on death.

1 Like