BindToClose does not fire on Reserved Server

Hello, i am working on a new datastore system and in a large server (300+ people) when Closing (Restarting for Update) this function does not fire and data of the player get lost.

This is actual the code wich i am using:

game:BindToClose(function()
	for _, Player in ipairs(PlayerService:GetChildren()) do
		if _G[Player.UserId] and _G[Player.UserId]["Stats"]["Saved"] == false and Player:FindFirstChild("PLAYER_STATS") and StudioMode == false then		

			local ArrayData = {
				["Currency"] = {
					["Cash"] = Player.PLAYER_STATS:GetAttribute("Money"),
					["Diamonds"] = Player.PLAYER_STATS:GetAttribute("Diamond"),
				},
				["UserData"] = {
					["PrisionTime"] = Player.PLAYER_STATS:GetAttribute("Prision_Time"),
					["EXP"] = Player.PLAYER_STATS:GetAttribute("EXP"),
				},
			};


			GAME_DB:UpdateAsync("User_" .. Player.UserId, function(OldData)
				return ArrayData;
			end)

			_G[Player.UserId] = {};
		end
	end
end)

Before anyone says the conditions aren’t true, I’m 100% sure the conditions are true before saving.

Hope someone can help me on this or have some solution for saving data in a reserver server when server is shutting down.

1 Like

Try adding task.wait(10) at the end of the function

this might happen because you teleport people before you save their data. Apperantly you are litterary teleporting everyone to other server without saving their data before the server closes, because when you teleport a player the server closes after some seconds. You need to upgrade ur code.

Where do you actually see that i am teleporting player? i do not use teleport service.

ur kicking them? or what you do

No Read the code, i am just saving their data when server close.

you need to use playerremoving, bindtoclose isn’t so recommanded to use

PleayerRemoved is used normally but when server shutdown you should use this function to save player data corretly, since PlayerRemoved When Server restart does not work / fire.

Wait try doing game.Players:GetPlayers() instead of this

Ok, So Right now it look with the wait the datastore issue is fixed, let’s hope that works also in a 300+ player server.

_G[] Is a global variable used in our game to cache some ranks and data and having it acessible in all the scripts, without bindableEvent.

for saving data normally we use
PlayerService.PlayerRemoving:Connect(function(Player) (When player leave)

instead when server is shutting down we use:
game:BindToClose(function()

This Global Variable _G[Player.UserId]["Stats"]["Saved"] is used in order to prevent double save of the same key in the same moment.

Hope that what im saying is correct and it’s the thing that works best!

I Put as solution the idea of @Redusofficial , If you have any improvements to make feel free to suggest them to improve the code/save.

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