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
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.