It’s a coins datastore, and the coins aren’t saving.
When a player leaves, their coins should be stored and then loaded when they rejoin. This isn’t the case here for some reason.
scripts:
(both in server script service)
script to setup the leaderstats
game.Players.PlayerAdded:Connect(function(player)
local lead = Instance.new("Folder")
lead.Name = "leaderstats"
lead.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = lead
end)
datastore script
local DSS = game:GetService("DataStoreService")
local dataStore = DSS:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local coins = player:WaitForChild("leaderstats").Coins
local data
local succ, err = pcall(function()
data = dataStore:GetAsync(player.UserId.."_coins", player.leaderstats.Coins.Value)
end)
if succ then
coins.Value = data
print("coins loaded")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = player.leaderstats.Coins.Value
local succ, err = pcall(function()
dataStore:SetAsync(player.UserId.."_coins", player.leaderstats.Coins.Value)
end)
if succ then
print("coins saved")
else
print("error when saving coins")
warn(err)
end
end)
Alright I have experienced this problem before, and you need to bind a close event to the DataModel to wait a few seconds. When the last player in the server leaves the game, the PlayerRemoving event might not run at all or only partially run, because the server closes straight after that.
I recommend adding a save button before the player leaves the game, or adding DataModel:BindToClose to bind a wait function that waits 10 seconds or until data is saved.
Check both the succ and err parameters on return from the Set/GetAsync(). The succ essentially means the DataStore was accessible, the err will tell you if the player has data or whether the calls successfully accessed the data.
Very simple, I will put it right here for you. Just add this into your script:
game:BindToClose(function()
wait(10)
end)
Of course, there’s a better way to do this but I think this works fine. The only issue with this is that Studio will wait long after exiting playtesting.
I would recommend an autosave feature instead, if you can code that in.