Answered by
4 years ago Edited 4 years ago
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.
1 | game.Players.PlayerRemoving:Connect( function (Player) |
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.
01 | game.Players.PlayerRemoving:Connect( function (Player) |
07 | Points = Player.leaderstats.Points.Value |
08 | Wins = Player.leaderstats.Wins.Value |
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:
03 | local DataStoreService = game:GetService( "DataStoreService" ) |
07 | local GameDataStore = DataStoreService:GetDataStore( "GameDataStore" ) |
09 | game.Players.PlayerRemoving:Connect( function (Player) |
15 | Points = Player.leaderstats.Points.Value |
16 | Wins = Player.leaderstats.Wins.Value |
20 | local PlayerUserId = "Player_" ..Player.UserId |
23 | local Success, ErrorMessage = pcall ( function () |
24 | GameDataStore:SetAsync(PlayerUserId, Data) |
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)