Saving inventory items on a table. Is that a good idea?

Hello. I am hired to make a game, and I need to create an inventory system. Yet I looked for good ways of saving inventory items without taking having to create an entire data store to each item. Yet, I am still unsure if that is a good idea.
What do you programmers think, is that a good idea to save inventory items on a table?
If yes or no please explain why you chose to answer that.

2 Likes

I can’t imagine any other way of doing it - so yes, it is a good idea I guess.

4 Likes

Yet, I still don’t understand how I can update the table.
Should I use DataStore:UpdateAsync()?

Yes and no. First off, you can also save players inventory using folders that are parented to the player. Then once the player left, you can get that data and save it.

Yes, that sounds like a sensible solution. UpdateAsync is always preferable since you can run integrity checks on the pre-existing data.

3 Likes

You shouldn’t update the table constantly, instead, you should do it when the player leaves or every minute or so.

2 Likes

Why would you do this? It’s unnecessary use of instances. The only benefit is Replication, at the expense of every other user being able to see everyone elses inventory.

4 Likes

How so? I want to save the entire inventory (and it’s sort as well), not X amount of items, table that will be like:

{Burger, Sushi, Burger, Burger, Cola}

(Of course this is just an example)

I find it easier using instances rather than actual code. Are there downsides to this? I haven’t heard any either.

It may be harder to do, you’ll have to specify that in your code which you want to be first. It’s best to use a table for that.

Should I do it like that?

local oldTable = dataStore:GetAsync(player.UserId.. "-inventory")

local earnedItems = {}; -- This is a table that handles all the items that the player has earned since he joined.

local newTable = {};

for _, v in ipairs(oldTable) do -- Looping through the old table.
   table.insert(newTable, #oldTable + 1, v)
end

for _, v in ipairs(earnedItems) do
   table.insert(newTable, #newTable + 1, v)
end

dataStore:UpdateAsync(player.UserId.. "-inventory", function()
   return newTable -- Returns the new table.
end

As I just said:

  • You’re going to be using deprecated instances (ObjectValues) to accomplish this
  • Increasing the number of instances has performance downsides (you’re storing big userdata objects in memory, and having to replicate those changes to everyone, even if it isn’t their inventory)
  • Leads to the potential for bugs relating to children not being present during loading, and can lead to concurrency issues
  • Poor security practice - why would you give every player access to everyone elses inventory? Any information you give out without need can be used by an attacker, even if you don’t realise how yet
2 Likes

Yes, although there is no need for a new table.

1 Like

Interesting. I’ll have to take a look into that and how I can reduce the number of instances, though I’ll keep some since they are important.