Need Help with game:BindToClose

I’m using a soft shutdown script that requires me to run game:BindToClose for it to shut down the server, but I can’t figure out how to get it to work. Does anyone know how to either: A: Use game:BindToClose or B: How to use the soft shutdown system

My Code:

--[[
	SoftShutdown 1.2
	Author: Merely
	
	This system lets you shut down servers without losing a bunch of players.
	When game.OnClose is called, the script teleports everyone in the server
	into a reserved server.
	
	When the reserved servers start up, they wait a few seconds, and then
	send everyone back into the main place.
	
	I added wait() in a couple of places because if you don't, everyone will spawn into
	their own servers with only 1 player.
--]]

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

if (game.VIPServerId ~= "" and game.VIPServerOwnerId == 0) then
	-- this is a reserved server without a VIP server owner
	local m = Instance.new("Message")
	m.Text = "Survive! is getting updated! Teleporting back in a moment."
	m.Parent = workspace
	
	local waitTime = 7

	Players.PlayerAdded:connect(function(player)
		wait(waitTime)
		waitTime = waitTime / 2
		TeleportService:Teleport(4520948628, player)
	end)
	
	for _,player in pairs(Players:GetPlayers()) do
		TeleportService:Teleport(4520948628, player)
		wait(waitTime)
		waitTime = waitTime / 2
	end
else
	game:BindToClose(function()
		if (#Players:GetPlayers() == 0) then
			return
		end
		
		if (game:GetService("RunService"):IsStudio()) then
			return
		end
		
		local m = Instance.new("Message")
		m.Text = "Rebooting servers for update. Please wait."
		m.Parent = workspace
		wait(2)
		local reservedServerCode = TeleportService:ReserveServer(4576150267)
		
		for _,player in pairs(Players:GetPlayers()) do
			TeleportService:TeleportToPrivateServer(4576150267, reservedServerCode, { player })
		end
		Players.PlayerAdded:connect(function(player)
			TeleportService:TeleportToPrivateServer(4576150267, reservedServerCode, { player })
		end)
		while (#Players:GetPlayers() > 0) do
			wait(1)
		end	
		
		-- done
	end)
end
1 Like

All game:BindToClose does is run code when the Server is Closing, In Real Servers, within the 30 Second Gap before Closing, and in Studio, Runs and Closes the game.

First, it checks if the server is a VIP server without an owner. If so, it displays a message to players joining the server and teleports them back to the game lobby. If not, it sets up a game:BindToClose function to handle server shutdown.

When the server receives a shutdown signal, the game:BindToClose function is called. It checks if there are any players in the game, and if not, it immediately returns. It also checks if the game is running in Roblox Studio, and if so, it also immediately returns.

If there are players in the game and the game is not running in Roblox Studio, it displays a message to all players and reserves a private server using TeleportService:ReserveServer(). It then teleports all players to the reserved server using TeleportService:TeleportToPrivateServer(). It also sets up a Players.PlayerAdded connection to automatically teleport any new players that join the game to the reserved server.

The script then waits in a loop until all players have left the game, at which point the server shuts down and the game:BindToClose function ends.

To use this script, you should ensure that it is placed in a ServerScriptService or a Script inside of ServerScriptService. This will allow the server to execute the game:BindToClose function when it needs to shut down.

To test if it works, go to your game page on Roblox, hit the 3 dots and then shut down all servers. (Make sure you’re testing in-game, not Studio!)

This does trigger it, BUT it kicks me before it will teleport me

Do you get a kick message, or does it just say you’ve been kicked?

I think I found my problem, should I be teleporting the players to a private server In my game or a different game?