It's better use more keys or more datastores?

The title seems confusing, i don’t know if it’s better use more keys in datastores, for example:

I don’t know if i have written this example correctly, tell me if i made something wrong please :slight_smile:

datastore:UpdateAsync(player.UserId..".Cash", data)
datastore:UpdateAsync(player.UserId..".Gems", data)
datastore:UpdateAsync(player.UserId..".Level", data)

Or use more DataStores:

local DataStore = game:GetService("DataStoreService")

local ds1 = DataStore:GetDataStore("CoinsData")
local ds2 = DataStore:GetDataStore("GemsData")
local ds3 = DataStore:GetDataStore("LevelData")

What you guys use instead? What you would recommend me to use? Thanks for reading :+1:

1 Like

I would recommend using tables. For ex.

local data = {Coins = 0; Gems = 0; Inventory = {item1 = {};};}
datastore:UpdateAsync(player.UserId, data)
8 Likes

Do you no longer need to convert the table data to Json before storing it?

No not anymore.
30 characters afasdkasjd

1 Like

Both examples aren’t very good to use, as you’re updating 3 asyncs to get cash and gaving to set 3 data stores for data. As @Dev_HDWC suggested, use tables instead. This is a way easier method and you only have to call GetAsync/UpdateAsync once.

Short answer: Neither.

Long answer:
Depending on your use case it is generally better to save/load all your players data under one key to avoid doing more data store calls than you need. Having multiple data stores will bring you the same problem because data store limits are per game not per data store. If you don’t use multiple data stores/keys correctly you will run the risk of hitting your data store limits very quickly.

Generally speaking the best way to save all the players data under one key is by having all their data under one table. Here is an example(the code below is only meant to be used as an example):

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Data")

local PlayersData = {
	Coins = 20;
	Level = 100;
	XP = 2000;
	Items = {};
}

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		DataStore:UpdateAsync(tostring(Player.UserId), function(OldData)
			return PlayersData
		end)
	end)
end)

Advantages of saving your data under one key:

  • You don’t need to do >10 data store calls, depending on your game, just to get and save your players data. Instead you can just do one data store call each time you want to save or get your data. This will reduce the amount of data store calls you do reducing the change of data loss and throttling.

  • It massively reduces the amount of data store calls you do. Remember data store calls sometimes fail and data stores have limits.

  • Handling failed data store requests become easier.


For as long as I can remember you never had to do this. You can save tables without them being converted.

If I can remember correctly I think @colbert2677 once said converting your table to Json only bloats the save. I could have made this up so please correct me if I am wrong.

3 Likes

You aren’t wrong. Unless you are using that JSON conversion to apply compression strategies, then JSON is unnecessary because that’s already done internally so the data can be transmitted in a format acceptable for DynamoDB. You’d just be tacking on unnecessary characters and adding an extra bit of work when either reading or writing data.

4 Likes