Datastore inefficiency problem

In one of my games, I’ve been getting reports of data loss. I’ve done my best to prevent it, and now I’m questioning my methods. My main question here is, what’s the best way to store multiple types of data?

For instance, I currently have 3 data stores. (I know in this case, three can be cut down to two, but it’s not done yet)

Is it better to do something like this:

		local ds = game:GetService("DataStoreService")
		local key = "UserID" .. player.UserId
		
		-- Stores
		local StatsStore = ds:GetDataStore("PlayerStats") 
		local ToolStore = ds:GetDataStore("PlayerTools")
		local GiftStore = ds:GetDataStore("PlayerGift")
		
		-- Keys
		local StatsToLoad = StatsStore:GetAsync(key)
		local ToolsToLoad = ToolStore:GetAsync(key)
		local GiftTime = GiftStore:GetAsync(key)

Or is it better to do something like this:

		local ds = game:GetService("DataStoreService")
		local key = "UserID" .. player.UserId
		
		-- Stores
		local Store = ds:GetDataStore("MasterStore") 
		
		-- Keys
		local StatsToLoad = Store:GetAsync(key .. "Stats")
		local ToolsToLoad = Store:GetAsync(key .. "Tools")
		local GiftTime = Store:GetAsync(key .. "Gift")

I personally wouldn’t do either of those. Instead, I would have a big table in a single DataStore key with most of the player’s data. Doing this helps decrease API requests by only sending one request for the saving/loading of data.

1 Like

So, like one master table with sub tables/arrays (I don’t know the proper terminology) for saving player data, tools, and other things?

I wouldn’t use any of those, you could use one datastore and save everything in one table. For preventing data loss, you can implement a BindToClose, which will save everyone’s data before a server shuts down - it could that the server shuts down before the player’s data could be saved. You can also try to implement an autosave feature that saves at intervals

1 Like

I have a BindToClose function, and that sure has helped, it’s more throttling issues.

Look into using DataStore2 by Kampfkarren, you can call the API to set a dictionary with all the values and store only that one dictionary, rather than creating multiple Datastores. This will help you in the long run to add more data as you please.

There’s no difference between these methods. The first one just bloats how many DataStores you’re using but they still end up using 3 requests per player which will exhaust your DataStore budget.

Use a dictionary for your data. Each key of the dictionary represents a different section of data and the value can branch out into other values or be a whole value itself.

local DataStoreService = game:GetService("DataStoreService")
local MasterDataStore = DataStoreService:GetDataStore("MasterStore")

local playerData = MasterDataStore:GetAsync(player.UserId)

Data should be saved in a formal something like the following:

local data = {
    ["Gift"] = false,
    ["Stats"] = {},
    ["Tools"] = {},
}
3 Likes