So i made this DataStore code that saves the players characters, this worked completely fine. Now that im trying to also add that the players Coins are getting saved, the whole thing just breaks down without returning any errors. I know in which line of code the problem is, i just dont know how to solve it.
Is there anyone out there that can help me?
This is the code:
local function playerRemoving(player)
local coins = player.leaderstats.Coins.Value
local _characters = {}
local succes, err = pcall(function()
for i,v in pairs(player.CharacterShop.Characters:GetChildren()) do
table.insert(_characters, v.Name)
end
task.wait(1)
datastore:SetAsync(player.UserId.."Characters", _characters)
datastore:SetAsync(player.UserId, coins)
end)
if succes then
print("Succes!")
else
warn("Error")
end
end
local function playerRemoving(player)
local data = {
["Characters"]={}
["Coins"] = player.leaderstats.Coins.Value
}
local succes, err = pcall(function()
for i,v in pairs(player.CharacterShop.Characters:GetChildren()) do
table.insert(data["Characters"], v.Name)
end
task.wait(1)
datastore:SetAsync(player.UserId, data)
end)
if succes then
print("Succes!")
else
warn("Error")
end
end
If u dont mind, how should i set up the loading part to also load the coins then?
Currently have this and i need to keep the characters part:
local _characters = nil
local succes, err = pcall(function()
_characters = datastore:GetAsync(player.UserId.."Characters")
end)
if succes and _characters ~= nil then
for i, v in pairs(_characters) do
local newVal = Instance.new("StringValue")
newVal.Name = v
newVal.Parent = ownedCharacters
print("Data has been load succesfully!")
end
else
warn(err)
end
end)
local data
local succes, err = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
if succes and data ~= nil then
for i, v in pairs(data) do
if i == "Coins" then
player.leaderstats.Coins.Value = v
else
for z,x in pairs(v) do
local newVal = Instance.new("StringValue")
newVal.Name = x
newVal.Parent = ownedCharacters
end
end
print("Data has been load succesfully!")
end
else
warn(err)
end
end)