Data Won't Save

^. I made a data store save multiple IntValues. Code:

local dataStore = game:GetService("DataStoreService")

local playerData = dataStore:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)

print("Player Joined Game")

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = player

local coins = Instance.new("IntValue")

coins.Name = "Coins"

coins.Parent = leaderstats

local wins = Instance.new("IntValue")

wins.Name = "Wins"

wins.Parent = leaderstats

local ID = "Player: " .. player.UserId

local data = playerData:GetAsync(ID)

if data then

coins.Value = data["Coins"]

wins.Value = data["Wins"]

else

coins.Value = 0

wins.Value = 0

end

end)

game.Players.PlayerRemoving:Connect(function(player)

print("Player Left Game")

local ID = "Player: " .. player.UserId

local data = {

player.leaderstats.Coins.Value;

player.leaderstats.Wins.Value

}

local success,err = pcall(function()

print("In Protected-Call Function")

playerData:SetAsync(ID, data)

end)

if not success then

warn("Couldn't Save Your Data! ): ")

else

print("Successfully Saved Your Data!! :) ")

end

end)

It’s printing what it’s supposed to, but not saving data. No errors in the output. I published it, then I played in the actual game. I turned on studio access to api services. Thanks! :wave:

1 Like

In that section there, you don’t define “Coins” or “Wins” in a dictionary, so it doesn’t retrieve those values from the table. To fix it, all you’d have to do is change your table to something like this

local data = {
  Coins = player.leaderstats.Coins.Value,
  Wins = player.leaderstats.Wins.Value
}

**Also, for posts you make in the future, please try to format your code so it’s easier for other people to read and help you :)**

4 Likes

I have the same problem… srry idk