Hi,
My data store script is simply not saving data, GetAsync returns nil. I don’t know why, it looks like everything is ok:
Players.PlayerAdded:Connect(function(player)
local leaderstats = createLeaderstats()
leaderstats.Parent = player
local wins = createWinsContainer()
local losses = createLossesContainer()
wins.Parent = leaderstats
losses.Parent = leaderstats
local data
local success, err = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if data == nil then
data = {["Wins"] = 0, ["Losses"] = 0}
end
wins.Value = data["Wins"]
losses.Value = data["Losses"]
end)
Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
DataStore:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
end)
end)
game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
local success, err = pcall(function()
DataStore:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
end)
end
end)
I’m not exactly sure what’s happening here, but what @vycVascense said is false; DataStores can save table values.
Can you send us the full source of your script? Or at least include the part where you’re defining DataStore.
And, pcalls returning their status and any error message isn’t just for safety, it can help you.
Print out your pcalls “err” variables in PlayerAdded, PlayerRemoving, and to be safe, in BindToClose.
Try testing it then and let us know what it said, if anything.
I took the liberty of running a basic test, and according to my results, there is absolutely 0 reason this shouldn’t work.
Here’s a lightly modified version of your supplied code, as well as proof of it saving.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("TempDefault")
local PlayerData = {}
local function createLeaderstats()
local a = Instance.new("Folder")
local b = Instance.new("IntValue")
local c = Instance.new("IntValue")
a.Name = "leaderstats"
b.Name = "Wins"
c.Name = "Losses"
b.Parent,c.Parent = a,a
return a
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = createLeaderstats()
leaderstats.Parent = player
local data
local success, err = pcall(function()
data = DS:GetAsync(player.UserId)
end)
print(("[DEBUG] [PlayerAdded] [%s] Player Data from GetAsync:"):format(player.Name), data)
if not success then
warn(("[ERROR] [PlayerAdded] [%s] Data failed to load/get, error message: \n%s\n"):format(player.Name, err))
end
if data == nil then
data = {["Wins"] = 0, ["Losses"] = 0}
end
leaderstats.Wins.Value = data["Wins"]
leaderstats.Losses.Value = data["Losses"]
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
DS:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
end)
if not success then
warn(("[ERROR] [PlayerRemoving] [%s] Data failed to be saved, error message: \n%s\n"):format(player.Name, err))
end
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
DS:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
end)
if not success then
warn(("[ERROR] [BindToClose] [%s] Data failed to be saved, error message: \n%s\n"):format(player.Name, err))
end
end
end)
Proof: (printed right after “data” is set to GetAsync)
Try this out (at least the printing) and let us know if anything comes up.
Edit: And if this is happening when testing in Studio, do be sure you actually have “Enable Studio Access to API Services” enabled in your Game Settings.