Why is OrderedDataStore not storing data when the player leaves?
Asked by
4 years ago Edited 4 years ago
This question has been solved by the original poster.
01 | local Players = game:GetService( "Players" ) |
03 | local DataStoreService = game:GetService( "DataStoreService" ) |
05 | local secondsStore = DataStoreService:GetOrderedDataStore( "secondsStore" ) |
07 | Players.PlayerRemoving:Connect( function (player) |
08 | print ( "Player leaving set datastore: " ..player.leaderstats [ "Time Played" ] .Value) |
09 | local success, error = pcall ( function () |
10 | secondsStore:SetAsync(player.UserId, player.leaderstats [ "Time Played" ] .Value) |
17 | local pages = secondsStore:GetSortedAsync( false , 8 ) |
18 | local data = pages:GetCurrentPage() |
19 | for i, v in pairs (data) do |
20 | print (v.key.. " - " ..v.value) |
Nothing in the datastore changes when the player leaves the game. I have API services enabled in Studio. The print line gives the right number but it doesn't go into the datastore. What am I doing wrong?
Edit: after some testing, I think the problem is that when the last player leaves, the server instantly shuts down before the data is saved. How can I keep the server running until all the data is saved?