Datastore's give up saving on game close

For some reason, when I close the game, my data stores don’t save, and instead it just ignores the saving line of code and just continues the script.

local function saveData(plr)
	if onSaveCooldown[plr] then return end
	onSaveCooldown[plr] = true
	local success, errorMessage = pcall(function()
		print("run hello")
		print(playerData[plr]) --print the data to make sure it exists
		playerDataStore:SetAsync(tostring(plr.UserId) .. "|" .. curDate,playerData[plr])

	if gameClosing then task.wait(5) return end
		onSaveCooldown[plr] = false

Code that calls the function

	gameClosing = true
	for _,plr in game.Players:GetPlayers() do
	print("done saving everyhting")

Expected output:

“run hello”
prints the correct data
true nil
“done saving everything”

Actual output:

“run hello”
prints the correct data
“done saving everything”

The weird thing is that if I call that function during runtime, then it works fine. But specifically on game close it doesn’t work.
Any advice?
Removing the p-call doesn’t do anything the script still stalls at the setAsync
I’ve changed to UpdateAsync but it still has the same problems(doesn’t even run the code inside the passed function as well)

My guess is that the server is closing before the save is finished, but it doesn’t explain the fact that it prints “don’t saving everything”. It should just print the first two, no?

It seems the issue is only in Roblox Studio, so I’ll just need to manually save when in Studio but that’s alright.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.