Save and load multiple data?

So, I’m currently trying to save two different data at once. I’m not sure how to do this correctly. At the moment, what I have done below seems incorrect and it doesn’t load the data.

--// Code: 
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local UpgradeData = DataStoreService:GetDataStore("PlayerData", "Upgrades")

local function CurrencyData()
	return {
		Stats = {
			Money = 0
		 }
	}
end

local function UpgradeData()
	return {
		Upgrades = {
			Stone = true,
			Diamond = false
		 }
	}
end

local function loadData(player)
	local Success, Data = pcall(function() return PlayerData:GetAsync("data:"..player.UserId) end)
	ServerData[player] = Success and Data or CurrencyData()
	local Success, Data = pcall(function() return UpgradeData:GetAsync("data:"..player.UserId) end)
	ServerData[player] = Success and Data or UpgradeData()
end

local function saveData(player)
	if ServerData[player] then
		local Success, Error = pcall(function() PlayerData:SetAsync("data:"..player.UserId, ServerData[player]) end)
		local Success, Error = pcall(function() UpgradeData:SetAsync("data:"..player.UserId, ServerData[player]) end)
		if not Success then
			warn("Data wasn't saved for "..player.Name..".")
			return
		end
		ServerData[player] = nil
	end
end
1 Like

It’s most likely due to the Asyncs not being get fast enough, therefore not giving the player their data.
Instead of having two Datastores you save data to, just merge the Datastores into one.

1 Like

Try out datastore2. It’s extremely easy. How to use DataStore2 - Data Store caching and data loss prevention. You can use this link DataStore2 to explore how to use it. You would used the combined data stores option in datastore2 in order to do this effectively and prevent throttles.

2 Likes

How would I save and load data for both datastores successfully?

You could just use one datastore. I would suggest using this video to help. https://youtu.be/at9Yhjt572c