SetAsync not working in PlayerRemoving

Hello. I have this short and simple script in my game:

game.Players.PlayerRemoving:Connect(function(plr)
	local succes, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."-Cash", plr.leaderstats.Cash.Value)
	end)
	if succes then
		print("Saved cash data.")
	else
		print("Didin't save cash data.")
		warn(errormessage)
	end
end)

The issue I’m having is that the script all of a sudden stopped working. It was working fine like 5 days ago and the game has been untouched since then.
I tried putting the script in an empty baseplate (with api services on) and it still doesnt print anything or save the data. Is datastore service down?

Do the print() statements execute? Any output messages?

If not, it might just be that the game closes before it has time to save the data. Try adding this at the bottom of the script:

game.Players.PlayerRemoving:Connect(save) --saving function here, this line is just for reference

game:BindToClose(function()
    if game["Run Service"]:IsStudio() then task.wait(5) return end
    for _, player in pairs(game.Players:GetPlayers()) do
        save(player) --saving function here
    end
    task.wait(5)
end)

BindToClose() will run any provided code before the game closes.

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