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])
		print("wow")
	end)
	print(success,errorMessage)

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

Code that calls the function

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

Expected output:

“run hello”
prints the correct data
“wow”
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.