Teleporting Between Places

If you want to build an experience with many distinct places, such as a fantasy world with multiple towns, castles, dungeons, and a vast forest, you can use TeleportService to enable users to teleport between places in a universe, servers, or even to another experience.

Setting Up Teleportation

To enable teleportation in your experience, use TeleportService:TeleportAsync(). The method accepts three parameters:

  • The PlaceId for users to teleport to.
  • An array containing the Player instances representing the users to teleport.
  • An optional TeleportOptions instance that contains custom properties for the TeleportAsync() call.

local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local TARGET_PLACE_ID = 1234 -- replace with your own place ID
local playerToTeleport = Players:GetPlayers()[1] -- get the first user in the experience
TeleportService:TeleportAsync(TARGET_PLACE_ID, {playerToTeleport}, teleportOptions)

If you want to take precautions of handling errors when setting up teleportation, see Handling Failed Teleports.

Enabling Cross Experience Teleportation

For security purposes, teleporting a user from your experience to another experience owned by others, or the other way around, fails by default. To enable cross experience teleportation, open Game Settings > Security and enable Allow Third Party Teleports on Studio.

Creating Custom Teleport Screens

When a user triggers a teleport, they see the standard Roblox loading screen as they wait for the new place to load in. You can add a custom teleport screen to improve immersion for users by calling TeleportService:SetTeleportGui() on the client and pass through the ScreenGui to use before teleporting the user. The following example sets a customized ScreenGui located in ReplicatedStorage as the loading screen when a teleport happens. It doesn't run any scripts inside of the ScreenGui.


local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local teleportGui = ReplicatedStorage.TeleportGui
TeleportService:SetTeleportGui(teleportGui)

Customizing Teleport Options

You can customize teleportations, such as teleporting users to a specific server and sending user data along with teleports, by setting the TeleportOptions instance and passing it to the TeleportService:TeleportAsync() method.

Teleporting to Specific Servers

To teleport users to specific servers, set the target server using TeleportOptions and pass it to the TeleportService:TeleportAsync() method. If you don't specify a server, users are teleported into a matchmade public server. The information of the first user in the list is used to matchmake to that public server.

To teleport users to a specific public server, set the TeleportOptions.ServerInstanceId property as a valid instance ID, which is a unique identifier for a public server.


local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ServerInstanceId = targetServerId

To teleport users to a specific reserved server, set a valid TeleportOptions.ReservedServerAccessCode, which is a unique code for entering a reserved server.


local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerAccessCode = reservedServerCode

To teleport users to a new reserved server, set TeleportOptions.ShouldReserveServer to true.


local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ShouldReserveServer = true

Sending User Data Along with Teleports

Teleporting a user between places discards any local data associated with that user. You can use the following approaches to handle data persistence between places.


local teleportData = {
randomNumber = RNG:NextInteger(1, 100);
}
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions:SetTeleportData(teleportData)

To get all data of a user arriving from a teleport on the server, use the Player:GetJoinData() function, which returns a dictionary including the data associated with the user.


local Players = game:GetService("Players")
local function onPlayerAdded(player)
local joinData = player:GetJoinData()
local teleportData = joinData.TeleportData
local randomNumber = teleportData.randomNumber
print(player.Name .. "joined with the number" .. randomNumber)
end
Players.PlayerAdded:Connect(onPlayerAdded)

To retrieve only the teleport data on the client, you can use TeleportService:GetLocalPlayerTeleportData().

Handling Failed Teleports

Like any API call that involves network requests, teleports might fail and throw an error. Even if a call succeeds and the teleport initiates, it can still fail at the last moment without throwing an error and leave the user in the server. When this happens, it triggers TeleportService.TeleportInitFailed.

Wrap teleports in a protected call (pcall()) and retry if it fails. The following example ModuleScript defines a SafeTeleport function to teleport the user in a protected call and a handleFailedTeleport function to retry failed teleports that are one-time hiccups and drops invalid ones that might have errors in the code.


local TeleportService = game:GetService("TeleportService")
local ATTEMPT_LIMIT = 5
local RETRY_DELAY = 1
local FLOOD_DELAY = 15
local function SafeTeleport(placeId, players, options)
local attemptIndex = 0
local success, result -- define pcall results outside of loop so results can be reported later on
repeat
success, result = pcall(function()
return TeleportService:TeleportAsync(placeId, players, options) -- teleport the user in a protected call to prevent erroring
end)
attemptIndex += 1
if not success then
task.wait(RETRY_DELAY)
end
until success or attemptIndex == ATTEMPT_LIMIT -- stop trying to teleport if call was successful, or if retry limit has been reached
if not success then
warn(result) -- print the failure reason to output
end
return success, result
end
local function handleFailedTeleport(player, teleportResult, errorMessage, targetPlaceId, teleportOptions)
if teleportResult == Enum.TeleportResult.Flooded then
task.wait(FLOOD_DELAY)
elseif teleportResult == Enum.TeleportResult.Failure then
task.wait(RETRY_DELAY)
else
-- if the teleport is invalid, report the error instead of retrying
error(("Invalid teleport [%s]: %s"):format(teleportResult.Name, errorMessage))
end
SafeTeleport(targetPlaceId, {player}, teleportOptions)
end
TeleportService.TeleportInitFailed:Connect(handleFailedTeleport)
return SafeTeleport

The SafeTeleport function receives the same arguments as the TeleportAsync() function. You can use the following ModuleScript with the SafeTeleport function to perform teleports from anywhere in your experience to reduce failed teleports.


local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local SafeTeleport = require(ServerScriptService.SafeTeleport)
local TARGET_PLACE_ID = 1818 -- replace with your own place ID
local playerToTeleport = Players:GetPlayers()[1] -- get the first user in the game
SafeTeleport(TARGET_PLACE_ID, {playerToTeleport}, teleportOptions)