Using 1 DataStore for many different values?

So I recently heard (I think it was in a TDK video) about the fact that you should use 1 data store or ordered data store for your entire game. So I have 3 questions to whoever replies:

  1. Do you suggest using only one DataStore?

  2. Do you use only one DataStore in all of your games?

  3. How would GetAsync() work with multiple pieces of data per key?

1.This depends entirely on your situation.One DataStore is usually all that is needed, however for things such as in game backup logs, a second data store under a custom key wouldn’t hurt anything.
2. There is a limit to the amount of data that can be called for at the same time, however you can still use more than one datastore. It’s not particularly sound programming to save to an egregious amount of datastores, and is probably not a good habit for retention.
3. You can set however many values you want (within the nearly unreachable limit) to a single table, and save the table to the Datastore, which is how practically every single game handles saving data. You set the stats, the items, the weapons, the player skills, their clothing IDs to table thedataTable
Then you just save the table to the player ID.

@MFCmaster1234 okay thanks for the information. I was confused about how you would mention information in a dataStore if there was more than one, because I usually just use game:GetService("DataStoreService"):GetDataStore("DataStoreName"):GetAsync(player.UserId)
So how would I reference it if there were multiple pieces of data?

Since you have saved a table, in this Instance, to the DataStore with the Key player.UserID, you can just set all of your above code to a variable, then access the values within that table the same way you would any.
tableName["Items"]["Potions"]["HealthPotion"]["Full Health Potion"]
and that will return the value of

tableName = {
  ["Items"] + {
     ["Potions"] = {

        ["HealthPotions"] = {
            ["Full Health Potion"] = 3
             }
        }
  }

Technically, this sort of table is called a dictionary because you are using Strings as keys.

So to reference it if you were trying to read from it you would do something like

game:GetService("DataStoreService"):GetDataStore("DataStoreName"):GetAsync(player.UserId,Items,Potions)

I’m pretty sure this is wrong, so I’d like to know exactly how you’d write it like this.

Okay to clarify. You would save

game:GetService("DataStoreService"):GetDataStore("DataStoreName"):GetAsync(player.UserId)
to a variable

local thisPlayersTable = game:GetService("DataStoreService"):GetDataStore("DataStoreName"):GetAsync(player.UserId)

But also, you should probably use variables more often for repeated lines of code.

local DataStoreService = game:GetService("DataStoreService")
local thisGameDataStore = DataStoreService:GetDataStore("DataStoreName")
local playerTable = thisGameDataStore:GetAsync(player.UserId)
local fullhealthpotions = tableName["Items"]["Potions"]["HealthPotion"]["Full Health Potion"]
print(fullhealthpotions)

Keep in mind I specifically named the variables very long names, but name them something you will remember, and you only ever have to access the actual variable set to the game’s data store, and call getasync on that variable, instead of typing out the whole thing you typed out each time.

Okay I usually don’t type out that entire thing when mentioning a DataStore. I was just trying to give an example. So if you wanted to change and save only one bit of data, you would have to change it in the table, and then save the entire table?

Yes, that is correct, however I would recommend simply setting an autosave feature up, and whenever you change a player value in game, save it to a table which updates whenevr you want it to, then save THAT table. This way you don’t have to call GetAsync and SetAsync every single time. You can quite simply just save the table every blank minutes or whenever someone buys something important. This allows you to restrict the amount of saving happening so you do not go past the datastore save rate.

1 Like