Rejoin script keeps resetting disconnecting and resetting progress

I have a button in my game that when pressed, rejoins you into the same game, a rejoin button. The issue I’m having is that whenever its pressed, 90% of the time I get error code 285: client initiated disconnect. and then 50% of the time when that happens, it resets the players leaderstats. Keep note my game is small and more often than not, there is only 1 person in the server doing the rejoins, I don’t know if that has anything to do with it.

here is my rejoin script, I can’t seem to find any better or more efficient scripts on devforum, every post is just this:


Any type of help is greatly appreciated.

You can’t use TeleportService in studio.

If you need leaderstats to save/load you’ll never to make use of a DataStore, alternatively you can send teleport data.

I’m not talking about testing in studio. I’m talking about playing in the actual game

I have a datastore set up already, and I’ve tried to have the datastore save when pressing the rejoin button, but whether or not that works, it still sets your stats back to zero once you’ve rejoined with the button

Here is the issue:

once the last player in the server leaves the game, the server shutdowns. The shutdown takes different amount of time each time, which can cause the player trying to join a server which is currently being shutdown so that explains the error which occurs.

local players = game:GetService("Players")
local teleports = game:GetService("TeleportService")

	local privateServerCode = teleports:ReserveServer(game.PlaceId)
	teleports:TeleportToPrivateServer(game.PlaceId, privateServerCode, {player})

As stated, when you teleport the last player of a server that server is scheduled for shutdown, if the player is then teleported to that same server you will encounter the aforementioned error, to get around this you just need to create (reserve) an additional server of the same game and teleport the player there instead.