Datastore not having time to save

Hello everyone!

I have been using a data module for quite a long time now, but I’ve been experiencing issues with it lately.
For some reason, sometimes the player’s data doesn’t save. I have no clue what causes this to happen. My guess is that the data doesn’t have time to save before the server shuts down. But how do I fix this? How do I make sure the UpdateAsync() function has enough time to save? Here’s my code:

Here’s the function that saves the player data:

function datamodule:UpdateData(player)
	local playerUserID = "DataOf_"..player.UserId

	local success, errorMessage = pcall(function()
		DS:UpdateAsync(playerUserID, function(oldData)
			if oldData then
				if oldData.Playtime and oldData.Playtime > sessionData["DataOf_"..player.UserId].Playtime then
					print("OLD")
					return oldData
				end
			end

			return sessionData[playerUserID]
		end)
	end)
	
	print(success, errorMessage)
end

Here’s the event that saves the player data when he leaves:

game.Players.PlayerRemoving:Connect(function(playerRemoving)
	-- server.unloadPlayer(playerRemoving)
	data:UpdateData(playerRemoving)
end)

Note: I am sure that the data:UpdateData() function is being called. So the issue has to come from the function that saves the player data.

1 Like

People would Suggest you use game:BindToClose() when it comes to making sure Data actually saves, and the game doesnt just close without firing PlayerRemoving

You need to use BindToClose. I’ve explained it in an earlier thread.

Here is a detailed post describing how it works.

Hello! I already have that. I know for a fact that the function to save the data is always being called, but it just doesn’t have enough time to finish executing before the server shuts down.

the max time a server can stay open after all players have left via bindtoclose is 30 seconds.

if your script cant save the data within that time its safe to say that time isnt the problem.

A problem i’ve faced before with a datastore module using profile service, is that the data gets released before im finished using it, the solution was to simply put a wait attribute and wait for it to be set to false.

local function OnPlayerRemoved(player)
	local PlayerProfile = Profiles[player]

	if PlayerProfile then
		repeat wait() until not player:GetAttribute('Wait')
		PlayerProfile:Release()
	end 
end

if your script is similar i would recommend doing something akin to that. otherwise i cant be of help here.

You forgot to validate the data while in the BindToClose function. GetAsync() the data until it matches what the data is supposed to be. Do that for all playes, and then close the server.