game:BindToClose() teleports do not work on soft shutdown or server restart

Whenever I try to restart servers in an experience to a newer version of the place with game:BindToClose() and by using TeleportService, the teleport fails by teleporting the user to the starting place rather than the place they were in. As a workaround, I tried to include the PlaceID in the TeleportData so I could just re-teleport them from the starting place, but that doesn’t work either, as the restart feature did not transfer TeleportData to the starting place. Makes it really inconvenient to restart servers if the data does NOT transfer over

Here’s what I have for my code:

game:BindToClose(function()
	if RunService:IsStudio() then return end
	local CurrentPlayers = PlayerService:GetPlayers()

	if #CurrentPlayers == 0 then return end

	task.spawn(function()
		for _, plr in CurrentPlayers do
			local UI = ServerStorage:FindFirstChild("SoftShut")
			if UI then UI:Clone().Parent = plr.PlayerGui end
		end
	end)

	local playersInfo = {["StadiumID"] = game.PlaceId }

	local TeleportOptions = Instance.new("TeleportOptions")
	TeleportOptions:SetTeleportData(playersInfo)
	
	task.wait(2)

	TeleportService:TeleportAsync(game.PlaceId, CurrentPlayers, TeleportOptions)
end)

A video of the issue:

Console print showing the Hub didn’t even get the teleport data:
image

Expected behavior

When I use the Restart Server function, I expect TeleportService to work with :BindToClose and teleport to the appropriate place under an experience while also transferring the teleport data.

Wait, so let me see if I understood. Please help me understand if I am wrong.
You want to teleport the player when the server closes?

If that’s so, you can’t do what you are thinking of. The server closes AFTER every player leaves.
When the server closes, you trying to iterate through the players, but there are no more in the game.

1 Like

The provided sample code seems reasonable to me. TeleportAsync in BindToClose should work fine for cases where you’re triggering server shutdown yourself (i.e. server is not shutting down because it’s empty). Double check that the linked code is actually in your non-root Place and not only in your root Place. If it is, we’ll see if we can repro/fix during the week

On the website, roblox has a feature to “shut down all servers” or “restart servers.” Im pretty sure “Shut down all servers” just performs a restart as I noticed that it does the exact same as migrating servers. So no matter what button I use, “Shut down all servers”, “Migrate to latest update”, or using the restart function in the server management tab in the Dashboard all restart servers.

Now that’s where the problem comes in ! Because the servers are getting restarted, it doesn’t teleport the player to the right place !!!

BindToClose() works perfectly as long as the player was in the starting place. however, when they aren’t, that’s when it doesn’t work …

Yep, the code is in both the non-root places and the root place (assuming root = starting). I’ve been changing stuff here and there to see what could be the issue, but unfortunately nothing really seems to fix it

1 Like

You’re close. Shut down all servers basically means that all the players inside it will get kicked out and won’t get back in. You need to teleport the player to a subplace, then to the starting place.

That’s really odd, considering when I use the “Shut down all servers” button, the player rejoin automatically, even after I’ve disabled the script that handles BindToClose() … Very interesting note for sure !

Are you sure it isn’t just the players rejoining manually?

Pretty positive, as I was rejoined automatically when i performed the action ..

That’s uhh. Odd.
Did you look at the documentation?https://create.roblox.com/docs/reference/engine/classes/DataModel#BindToClose

We’re phasing out “Shutdown All Servers” / “Migrate” etc as they’ve caused quite a bit of confusion. The only difference between all the options these days is which servers get shut down rather than how they get shut down (the new Server Management page makes this much clearer)

BindToClose + Teleport is a common pattern and should work well when restarting servers. So either that is broken, or something above the Teleport in the BindToClose example is throwing an error. If you’d like to really make sure it’s not your scripts, try removing everything other than the teleport and simplify it to just pass PlaceId and Players

I have a question, is BindToClose() called when all the players leave the server?

Yes, but not directly. It’s called whenever a server shuts down. Roblox shuts down servers whenever they are empty. Servers don’t shut themselves down when they are empty - they wait for Roblox to confirm that they are empty and no one is about to join

Interesting, always wondered what that method even was.
But, if there are no players inside the game when BindToClose() is called, what’s the point in teleporting every player to another place? There aren’t any players there…

If the server is shutting down because it’s empty, there is no point in trying to teleport. However, in cases where the server is shutting down and it’s not empty (such as when restarting servers yourself), it allows you to choose what behavior you want. If you’re restarting servers yourself and you do nothing, players will automatically teleport back to the root Place. What OP is trying to do is keep them in the non-root Place, which is a valid use case

2 Likes

Oh. That’s what they wanted. Probably it is a glitch then.

But thanks for clarifying what BindToClose is :smiley:

1 Like

okay, so as it turns out I’m a bad programmer … error in teleporting that would show up at the VERY last minute and i just caught it ! my issue is fixed now, user-error and not an engine bug … Sorry !

1 Like

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