Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is OrderedDataStore not storing data when the player leaves?

Asked by 3 years ago
Edited 3 years ago

This question has been solved by the original poster.
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?

1 answer

Log in to vote
0
Answered by 3 years ago

I figured out how to fix this on my own. I found out that game:BindToClose will run when the server is about to shut down, so I loop through all the players and save their stats before the server goes down. The PlayerRemoving code still runs when someone leaves the server but they are not the last person to leave the server. If there is a god above, he weeps

Ad

Answer this question