PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

TeleportToPrivateServer

This function teleports one of more Player|Players to a reserved server created using TeleportService/ReserveServer.

The reservedServerAccessCode parameter is the access code returned by TeleportService/ReserveServer|ReserveServer.

TeleportToPrivateServer can only be called on the server.

Spawn name

An optional spawnName parameter can be provided, which will cause the Player|Players to initially spawn at the SpawnLocation of that name in the destination place. The SpawnLocation must be valid for the Player|Players to spawn on. For example, it must be SpawnLocation/Neutral|neutral or set to the same SpawnLocation/TeamColor|TeamColor as the Team the Player|Players will be assigned to upon joining the game.

Teleport data

A teleportData parameter can be specified. This is data the clients will transmit to the destination place and can be retrieved using TeleportService/GetLocalPlayerTeleportData.

The teleportData can take any of the following forms:

  • A table without mixed keys (all keys are strings or integers)
  • A string
  • A number
  • A bool

As the teleportData is transmitted by the client it is not secure. For this reason it should only be used for local settings and not sensitive items (such as the users’ score or in-game currency).

If you need teleport data to persist across multiple teleports, you can use TeleportService/SetTeleportSetting and TeleportService/GetTeleportSetting.

Loading screen

A customLoadingScreen argument can be specified. This is a ScreenGui that is copied (without scripts) into the CoreGui of the destination place.

You are advised to instead set the loading screen on the client using TeleportService/SetTeleportGui. The loading ScreenGui should also be parented to the Player|Player’s PlayerGui a few seconds before the teleport to ensure a smooth transition using a RemoteEvent. For an example of this see TeleportService/SetTeleportGui|SetTeleportGui

The loading ScreenGui can be obtained in the destination place using TeleportService/GetArrivingTeleportGui, where developers can parent it to the PlayerGui.

Teleport failure

In some circumstances a teleport may fail. This can be due to the developer configuring the teleport incorrectly or issues with Roblox’s servers.

  • If a teleportation request is rejected the TeleportService/TeleportInitFailed event will fire the error message and a Enum/TeleportResult enumerator describing the issue
  • Teleports can fail ‘in transit’, after the user has left the server, due to issues with Roblox’s servers. In this case the user will be shown an error message and be required to rejoin the game

For more information on how to teleport players, see the Articles/Teleporting Between Places tutorial.

Parameters

Name Type Default Description

The ID of the place to teleport to

The reserved server access code returned by TeleportService/ReserveServer

An array of Player|Players to teleport

Optional name of the SpawnLocation to spawn at

Optional data to be passed to the destination place. Can be retrieved using TeleportService/GetLocalPlayerTeleportData

nil

Optional custom loading screen to be placed in the CoreGui at the destination place. Can be retrieved using TeleportService/GetArrivingTeleportGui

Returns

Return Type Summary

Code Samples


TeleportService: Teleport to a Reserved Server via Chat

The following code would reserve one server, if it hasn’t be reserved before. Whenever someone says “reserved” he/she will be teleported to the private server.

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetGlobalDataStore()
 
-- Get the saved code
local code = DS:GetAsync("ReservedServer")
if type(code) ~= "string" then -- None saved, create one
	code = TS:ReserveServer(game.PlaceId)
	DS:SetAsync("ReservedServer",code)
end
 
local function Joined(plr)
	-- Everytime they chat, we want to know
	plr.Chatted:Connect(function(msg)
		if msg == "reserved" then -- Aha, that's our cue
			TS:TeleportToPrivateServer(game.PlaceId,code,{plr})
		end
	end)
end
 
-- Connect all current and future players
Players.PlayerAdded:Connect(Joined)
for k,v in pairs(Players:GetPlayers()) do
	Joined(v)
end

TeleportService: Teleport to a Reserved Server

The following code would send everyone in the current game to a reserved server. Since reserved servers can only be joined by using /TeleportService/TeleportToPrivateServer, nobody will join the reserved server afterwards.

Mind that, since everyone will be teleported, the current server will (probably) shutdown as nobody would be left in it.

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

local code = TS:ReserveServer(game.PlaceId) -- Returns a code
local players = Players:GetPlayers() -- Get a list of all players

TS:TeleportToPrivateServer(game.PlaceId,code,players) -- Actually teleport the players
-- You could add extra arguments to this function: spawnName, teleportData and customLoadingScreen