There are a couple of things that are doing wrong, but as for the thing not loading its because there isn’t something known as “ObbyBux” or “Finished” in the data table while loading, because while you save it, you save it with numerical indices.
So your new script should look like this:
local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetDataStore(“playerdata”)
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "ObbyBux"
cash.Parent = folder
local xp = Instance.new("IntValue")
xp.Name = "Finished"
xp.Parent = folder
local playerId = "player_"..player.UserId
local success, response = pcall(playerdata.GetAsync, playerdata, playerId) --You can just do a pcall like this instead of making another anonymous function inside it and then calling GetAsync again inside of it.
if success then
cash.Value = response[1] or 0 --I normally do this because, if data isn't there in dataStore of the player, it will be set to 0 by default.
xp.Value = response[2] or 0
else
warn(response)
xp.Value = 0
cash.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player.leaderstats
local datatostore = {
leaderstats.ObbyBux.Value;
leaderstats.Finished.Value;
}
local playerId = "player_"..player.UserId
local success, errormessage = pcall(function()
playerdata:SetAsync(playerId, datatostore)
end)
if success then
print("Successfully Saved data!")
else
warn(errormessage)
end
end)
game:BindToClose(function()
wait(5)
end)
You can see what I also did with the pcall for getting the data, you can do the same for it in SetAsync & also that thing that you’re doing in BindToClose function, isn’t really a good practice.
This thread here is very useful in explaining it: