local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local secondsStore = DataStoreService:GetOrderedDataStore("secondsStore")
Players.PlayerRemoving:Connect(function(player)
print("Player leaving set datastore: "..player.leaderstats["Time Played"].Value)
local success, error = pcall(function()
secondsStore:SetAsync(player.UserId, player.leaderstats["Time Played"].Value)
end)
if not success then
warn(error)
end
end)
local pages = secondsStore:GetSortedAsync(false, 8)
local data = pages:GetCurrentPage()
for i, v in pairs(data) do
print(v.key.." - "..v.value)
end
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?
