Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is OrderedDataStore not storing data when the player leaves?

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.