01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local myDataStore = DataStoreService:GetDataStore( "myDataStore" ) |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 |
06 | local leaderstats = Instance.new( "Folder" ) |
07 | leaderstats.Name = "leaderstats" |
08 | leaderstats.Parent = player |
09 |
10 | local Cash = Instance.new( "IntValue" ) |
11 | Cash.Name = "Cash" |
12 | Cash.Parent = leaderstats |
13 |
14 | local playerUserId = "Player_" ..player.UserId |
15 |
The problem is that when the last player in the game leave, the server will shut down immediately and this also happens the same when you shut down the server with players still in the game so you would need to make sure that their data is save. So what you should do is just add this code:
1 | game:BindToClose( function () |
2 | repeat |
3 | local player = game.Players:GetChildren() |
4 | wait( 1 ) |
5 | until #player = 0 |
6 | end ) |
Hope this help!