So, you’re going to need one datastore per player instead of the 4 you are using, you can totally add more values if you need to in the future.
For this, I would do something like this, I also recommend you save the type of value to create:
For loading:
local succ, errormsg = pcall(function()
data = Datastore:GetAsync(yourKey)
end
if succ and data ~= nil then -- if it succeeded and the player has played before (checking if the data is nil or not)
for i,v in pairs(data) do
local currentCards = Instance.new("Folder", yourFolder)
currentCards.Name = i -- This will be the index, let's say it's Chloe for example.
local cardOwned = Instance.new("BoolValue", currentCards) -- Create all of the values that go into Chloe's folder.
cardOwned.Name = "CardOwned"
cardOwned.Value = v.CardOwned
local cardAmount = Instance.new("IntValue", currentCards)
cardAmount.Name = "CardAmount"
cardAmount.Value = v.CardAmount
local level = Instance.new("IntValue", currentCards)
level.Name = "Level"
level.Value = v.Level
else -- if it succeeded but the data is nil, so the player hasn't joined before, same as above basically. Keep in mind that the values should be default. You are going to have to create your own folders for this though.
local availableCards = {"Chloe", "KEKI", "Radley", "Sabaku"} -- just to determine what cards you can get
for i,v in pairs(availableCards) do
local currentCards = Instance.new("Folder", yourFolder)
currentCards.Name = i -- makes a folder called "Chloe" for example.
local cardOwned = Instance.new("BoolValue", currentCards)
cardOwned.Name = "CardOwned"
cardOwned.Value = false
local cardAmount = Instance.new("IntValue", currentCards)
cardAmount.Name = "CardAmount"
cardAmount.Value = 0
local level = Instance.new("IntValue", currentCards)
level.Name = "Level"
level.Value = 0
end
end
else
warn("Error retrieving data for "..player.Name..": "..errormsg
end
For saving:
local dataToSave = {}
local myFolder = --[[wherever the player's data folder is]]
for i,v in pairs(myFolder:GetChildren()) do
dataToSave[v.Name] = { -- ["Chloe"] = {
["CardOwned"] = v:FindFirstChild("CardOwned").Value,
["CardAmount"] = v:FindFirstChild("CardAmount").Value,
["Level"] = v:FindFirstChild("Level").Value
}
end
local succ, errormsg = pcall(function()
DS:SetAsync(myKey, dataToSave)
end
To better visualize, here’s an example of how the data’s table looks:
local data = {
["Chloe"] = {
["CardOwned"] = true,
["CardAmount"] = 12345,
["Level"] = 123
},
["KEKI"] = {
["CardOwned"] = true,
["CardAmount"] = 12345,
["Level"] = 123
},
["Radley"] = {
["CardOwned"] = true,
["CardAmount"] = 12345,
["Level"] = 123
},
["Sabaku"] = {
["CardOwned"] = true,
["CardAmount"] = 12345,
["Level"] = 123
}
}