My save script doesn't find enough time to save it incase of game:BindToClose

  1. I wanna save player data incase of server shutdown

  2. It works for update shutdown but server shutdown doesn’t give enough time to save player data
    (Yes my save system works fast it just needs couple seconds)

game:BindToClose(function(Reason)
	for int, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			SaveSlot(player)
		end)
	end
end)

Are the players even still a part of the data model at the time of BindToClose?

Yes probably theres memory loss due not enough saving time let me check

BindToClose happens when the last player left and the server wants to shut down, so I doubt you will get any Players to save the data for in the loop

game:BindToClose(function(Reason)
	for int, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			SaveSlot(player)
		end)
	end
	task.wait(10) -- Force a wait so that everything is saved.
end)

Or you can simply remove the thread.

Try printing #Players:GetPlayers() at the start of BindToClose

Wow I didn’t know you can wait up whole server i thought BindToClose function was like connect event before server goes in shutdown Thanks! you are lifesaver

You’re welcome, it works like a waiting function. You can connect multiple BindToClose functions, and the server will close after all BindToClose functions have finished, but there is a time limit, if I’m not mistaken it’s 30 seconds.

1 Like

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