local U=game.PlaceId local C=game:GetService("\080\108\097\121\101\114\115")local y=game:GetService("\084\101\108\101\112\111\114\116\083\101\114\118\105\099\101")local L=script:WaitForChild("\084\101\108\101\112\111\114\116\083\099\114\101\101\110\067\114\101\097\116\111\114")local v=require(L)local e=script:WaitForChild("\083\111\102\116\083\104\117\116\100\111\119\110\076\111\099\097\108\083\099\114\105\112\116")L.Parent=e e.Parent=game:GetService("\082\101\112\108\105\099\097\116\101\100\070\105\114\115\116")local P=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")P.Name="\083\116\097\114\116\083\104\117\116\100\111\119\110"P.Parent=game.ReplicatedStorage if not(game.VIPServerId~=""and game.VIPServerOwnerId==0)then game:BindToClose(function()if#game.Players:GetPlayers()==0 then return end if game.JobId==""then return end P:FireAllClients(true)wait(2)local L=y:ReserveServer(U)local e=v()local function i(C)y:TeleportToPrivateServer(U,L,{C},nil,{IsTemporaryServer=true;PlaceId=U},e)end for U,C in pairs(C:GetPlayers())do i(C)end game.Players.PlayerAdded:connect(i)while#C:GetPlayers()>0 do wait()end end)end
--Preforms a "soft shutdown". It will teleport players to a reserved server
--and teleport them back, with a new server. ORiginal version by Merely.
--Be aware this will hard shutdown reserved servers.

local PlaceId = game.PlaceId

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

local TeleportScreenCreator = script:WaitForChild("TeleportScreenCreator")
local CreateTeleportScreen = require(TeleportScreenCreator)

local SoftShutdownLocalScript = script:WaitForChild("SoftShutdownLocalScript")
TeleportScreenCreator.Parent = SoftShutdownLocalScript
SoftShutdownLocalScript.Parent = game:GetService("ReplicatedFirst")

local StartShutdown = Instance.new("RemoteEvent")
StartShutdown.Name = "StartShutdown"
StartShutdown.Parent = game.ReplicatedStorage

if not (game.VIPServerId ~= "" and game.VIPServerOwnerId == 0) then
	--If it is not a reserved server, bind the teleporting on close.
	game:BindToClose(function()
		--Return if there is no players.
		if #game.Players:GetPlayers() == 0 then
			return
		end

		--Return if the server instance is offline.
		if game.JobId == "" then
			return
		end

		--Send the shutdown message.
		StartShutdown:FireAllClients(true)
		wait(2)
		local ReservedServerCode = TeleportService:ReserveServer(PlaceId)

		--Create the teleport GUI.
		local ScreenGui = CreateTeleportScreen()
		local function TeleportPlayer(Player)
			TeleportService:TeleportToPrivateServer(
				PlaceId,
				ReservedServerCode,
				{ Player },
				nil,
				{ IsTemporaryServer = true, PlaceId = PlaceId },
				ScreenGui
			)
		end

		--Teleport players and try to keep the server alive until all players leave.
		for _, Player in pairs(Players:GetPlayers()) do
			TeleportPlayer(Player)
		end
		game.Players.PlayerAdded:connect(TeleportPlayer)
		while #Players:GetPlayers() > 0 do
			wait()
		end
	end)
end
