Understanding How a Inventory Works (Confused)

So I’m just trying to upgrade my scripting skills here if you know what I mean, but anyways I have some troubles I need to explain with creating an inventory.

  • How do I save something and place it in it’s own table using a DataStore? (Inventory)
  • What’s the difference between OrderedDataStore and DataStore?

These two questions have been holding me back (and yes they may not be correctly worded because I’m bad at explaining things), and I’d like to know if there’s anyone out there who can help me with them.

Sorry if some things are messed up I’m typing on mobile

OrderedDataStore vs DataStore

Regular DataStores, as you may know, are things where you can save data to in a “key:value” fashion. With these, the value can be anything, whether that’s a string, a table, or whatever.

OrderedDataStores are similar to DataStores except for the important fact that the values can only be positive integers. They do this so that the data can be sorted. So, if you tried to do OrderedDataStore:SetAsync(“Sam”, “Random string”), then it would error.

Inventory

You cannot save instances such as parts or meshes to a datastore, so generally what I see people do is save its characteristics like this:

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“USERID_Inventory”)
DataStore:SetAsync({
 {
  Name = “Celestial Sword”,
  Level = 9,
  Upgrades = 23
 },
 {
  —Another weapon
 }
})

Why would this error?

This works completely fine,

game:GetService"DataStoreService":GetGlobalDataStore():SetAsync("Sam","Random string")

Oh that’s odd. When I tried it a long time ago it errored.

edit: Wait never mind, I meant the ORDERED DataStore would error with that.

Thanks for showing me how to SetAsync with a table, but it’s inside of 2 tables so what can I do to check if a player has “Celestial Sword”? (Also I totally understand why each table is for a different item, I just want to know how to access/check if a player has them)

Other than that I now can understand the difference between OrderedDataStore and DataStore, so thank you for that.

You can just iterate through the table like

function getItem(ItemName)
  for i, item in pairs(inventory) do
    local Name = item.Name
    if (Name == ItemName) then
      return item
    end
  end
end

or something similar

??? I’m confused on how to access it still, I don’t understand how the function you made has anything to do with finding a certain item inside of the table.

I changed it to “GetItem” for clarity. Basically the inventory variable inside of pairs() is the table which represents the inventory, which is the table we used in SetAsync(). GetItem(ItemName) would search through every item in their inventory and check to see if any of the items in said inventory have the same name as ItemName. So, say if you wanted to retrieve “Fiery Dagger” from their inventory, you’d do

local FieryDagger = GetItem(“Fiery Dagger”)

which would then retrieve a table like

{
  Name = “Fiery Dagger”,
  Level = 9,
  ...
}

I’ll give this a try my friend, thanks for your help very much appreciated!

1 Like