Is it ok to use SetAsync twice for datastores?

Simply, are there any downsides to storing data by using multiple setasyncs in my datastore script?

My code works just fine and works as intended, but i am curious if there are future issues by doing this.

For example, in my game, im storing the data of the players backpack/inventory, and their player stats with two seperate datastores, instead of putting them all into one big table.

just wondering if theres any problems with data for storing it like this for data loss and such.

local player_stats = {}

	for _, stat in pairs(player.leader:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end

local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data
		
	end)


	for _, Tool in pairs (player.Backpack:GetChildren()) do
		
		if Tool:IsA("Tool") then
			
			table.insert(Backpack, Tool.Name)
			
		end
		

if Backpack ~= nil then
		
		local playerUserId = "Player_" .. player.UserId
		
		DataBackpack:SetAsync(playerUserId, Backpack)
		
	end
	

1 Like

Each experience and server has a budget of DataStore API calls per minute, depending on the number of players in a server. You can read about that on the wiki, under DataStoreService IIRC. Having twice as many SetAsync calls means you potentially exhaust your budget faster. Since these calls might fail randomly, having twice as many calls also increases the risk that one of them will fail, and since just a single failure causes your game to be in an inconsistent state, the chance of your entire game being broken for a player increases. E.g. if there’s a 1% chance of SetAsync failure (it’s MUCH lower) then there’s a 100% - (99% * 99%) = 1.99% chance of failure in your system that uses twice as many SetAsync calls.

You should also really handle failures in some other way than just supressing the errors with pcalls. The wiki shows examples of how you can make your system keep retrying if a call fails. And consider using a data saving library like DataStore2 that handles stuff like save versioning, auto saves and retrying for you.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.