Not sure how to save items in inventory

I am currently working on a game with a sandbox placement system. I have an inventory that the system uses located in the server, but I don’t know how to save it. I would figure you’d save it as an array, but I’m not sure how to save the value of the item as well as it’s name (IntValue). Does anyone have any methods they use or ideas they have they can share? Thanks!

2 Likes

With DataStores…?

If you want to save the name and value of something then you could use a dictionary instead. Make the key the name of something and change the value depending on your needs as either a single-value or another table containing information about the key.

2 Likes

If it were me:

I’d want to save the data in the most primitive form possible (integer). I would also want to save each objects location in the sandbox. For this reason, I would the sandbox broken up into a grid system, and every item would have their primarypart set to a part that takes up exactly 1 grid space. Then, when the time came to save the data, I would save it as an un-ordered list of tables. Example:

`local itemsToSave = {
   --itemName index    ItemGridPosition      otherItemValue

  { 1,                 1,                    1}
}`

Then, to retrieve the item’s name upon re-loading the data, I would have a module set up that stores all relevant item information, such as the items name, and load the items name based upon what integer I saved at index 1 of the datastore.

ItemList = {[1] = {"Table", 3, ...}}
loadedItemName = itemList[savedItemNameIndex]

hope this helps

2 Likes

So you’re suggesting that I save it as a table within a table? Also, how would I get the grid placement value to save? Would I just save it when it is placed in the placement system script? (I am using DataStore2 and the procedure is to save as changes occur rather than when the player leaves).

I assume you save it as a pair of two numbers inside of a table, since it’s a grid, where the first number is the X axis and second is Y axis.

{2, 5}

Or it can even be, if you want, stored as a Vector2

Vector2.new(2, 5)

Just have every grid position labeled with a numerical position (1, 2, 3, …) and save the integer for what position that primarypart of the item you’re saving is placed at.

I’m not sure that I understand your question, but maybe this post could help?