✓ Accepted answer
When he leaves you are obviously going to want to save the data. The best way to do this is in a PlayerRemoving function. A function that runs when the player leaves the game.
game.Players.PlayerRemoving:Connect(function(Player)
end)
The next thing your going to want to do is reference the data. I personally do not know how your data is stored so the next part will likely need to be edited depending on your situation.
game.Players.PlayerRemoving:Connect(function(Player)
local Data = {
Points = Player.leaderstats.Points.Value
Wins = Player.leaderstats.Wins.Value
}
end)
The final part you are going to want to do is save this to the player. To do this you are going to need a key. Something which you can identify for each individual player. You are then going to need to try and save that (doing it in a pcall in case it fails).
The final result should look something like this:
local DataStoreService = game:GetService("DataStoreService")
local GameDataStore = DataStoreService:GetDataStore("GameDataStore")
game.Players.PlayerRemoving:Connect(function(Player)
local Data = {
Points = Player.leaderstats.Points.Value
Wins = Player.leaderstats.Wins.Value
}
local PlayerUserId = "Player_"..Player.UserId
local Success, ErrorMessage = pcall(function()
GameDataStore:SetAsync(PlayerUserId, Data)
end)
if Success then
print("Data Saved.")
else
print(ErrorMessage)
end
end)
When loading the data you are obviously going to need to reference the key. However since this question was purely about saving data on removal I will only be answering that. (+ if I did much more I would practically be scripting your data save system)
1 comment
This is all I needed, thank you so much for taking your time and describing everything!