I really have no idea what I'm doing wrong! I have two functions: one for when a player enters, and one for a player leaves. Here's the one for players entering:
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue", player)
stats.Name = "leaderstats"
local hidden = Instance.new("IntValue", player)
hidden.Name = "hiddenstats"
local bounty = Instance.new("IntValue", stats)
bounty.Name = "Bounty"
local power = Instance.new("IntValue", stats)
power.Name = "Power"
local title = Instance.new("StringValue", stats)
title.Name = "Title"
title.Value = "None"
local justSpawned = Instance.new("BoolValue", hidden)
justSpawned.Name = "JustSpawned"
justSpawned.Value = false
local isDead = Instance.new("BoolValue", hidden)
isDead.Name = "IsDead"
isDead.Value = false
local currentHealth = Instance.new("IntValue", hidden)
currentHealth.Name = "CurrentHealth"
currentHealth.Value = 100
local onQuest = Instance.new("BoolValue", hidden)
onQuest.Name = "OnQuest"
onQuest.Value = false
local key = "Player-"..player.userId
local savedPlayerStats = PlayerData:GetAsync(key)
if savedPlayerStats then
bounty.Value = savedPlayerStats[1]
power.Value = savedPlayerStats[2]
title.Value = savedPlayerStats[3]
justSpawned.Value = savedPlayerStats[4]
isDead.Value = savedPlayerStats[5]
currentHealth.Value = savedPlayerStats[6]
onQuest.Value = savedPlayerStats[7]
else
local dataToSave = {bounty.Value, power.Value, title.Value, justSpawned.Value, isDead.Value, currentHealth.Value, onQuest.Value}
PlayerData:SetAsync(key, dataToSave)
end
end)
Here's the one for players leaving:
game.Players.PlayerRemoving:connect(function(player)
local key = "Player-"..player.userId
local stats = player:WaitForChild("leaderstats")
local hiddenstats = player:WaitForChild("hiddenstats")
local bounty = stats:WaitForChild("Bounty")
local power = stats:WaitForChild("Power")
local title = stats:WaitForChild("Title")
local justSpawned = hiddenstats:WaitForChild("JustSpawned")
justSpawned.Value = false
local isDead = hiddenstats:WaitForChild("IsDead")
isDead.Value = false
local health = hiddenstats:WaitForChild("CurrentHealth")
health.Value = 100
local onQuest = hiddenstats:WaitForChild("OnQuest")
onQuest.Value = false
local dataToSave = {bounty.Value, power.Value, title.Value, justSpawned.Value, isDead.Value, health.Value, onQuest.Value}
PlayerData:SetAsync(key, dataToSave)
end)
Please note that I did not include the code for creating the database. Anyways, I've read over my code like a thousand times, and I can't figure out what I'm doing wrong. I've tested it multiple times, manually changing my leaderstat values, but when I reenter the game, the values go back to 0.

11 comments
I think "leaderstats" should be a Folder or a Group object (not an IntValue) for it to display correctly in the PlayerList.
@KingedPawn Thank you for that note, but the problem still persists.
Are there any errors? If there are, I think you may need to use a pcall everytime you try to receive something from the DataStore. It may be that you're trying to retrieve too much information in a little amount of time.
Also, try to do some debugging with print statements. Try to print out savedPlayerStats[1] and savedPlayerStats[2] and so on to see what those values are.
@KingedPawn These are great suggestions. I just tried printing out values from savedPlayerStats, and nothing resulted in the output. I don't receive any errors either. This is really strange.
I used to have a similar problem with my game a long time ago, but I think I was able to fix it by switching to UpdateAsync instead of SetAsync. In your PlayerRemoving event, change the SetAsync to UpdateAsync (but keep it in your PlayerAdded one): http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/UpdateAsync
I did this: PlayerData:UpdateAsync(key, function(oldValue) oldValue = bounty.Value local newValue = oldValue end) My problem is still not fixed. I don't know if I'll ever be able to find out what's wrong.
Try it like this (you have to have a return value): PlayerData:UpdateAsync(key, function(oldValue) return dataToSave end)
@KingedPawn I believe the problem may lie somewhere in the function for players entering. I tried printing the values for savedPlayerStats, but nothing showed up in the output. I'm starting to doubt the fact that the player data is even loaded in the first place.
@KingedPawn I finally found the solution! I posted it as a comment to the answer of this question.
Congrats. Glad you found what was wrong.