Teleporting Between Places
Teleporting Between Places
Very large worlds may perform better when teleportation is implemented. For instance, if a fantasy world has several towns, a castle, multiple large dungeons, and a haunted forest, you should make each a separate place and teleport players between them at specific points (like a dungeon’s entrance/exit).
In Roblox, teleportation is handled by
TeleportService. This can be used to teleport players between places in a game or even to another game.
Getting a Target ID
Most of the functions in
TeleportService require the ID of the target place or game. If the destination place is in the same game as the origin place, the ID can be obtained from the Game Explorer by expanding the Places tree, right-clicking the place, and selecting Copy ID to Clipboard.
Alternatively, if the teleport destination is another game, the ID can be copied from the URL of game’s main page on roblox.com.
Valid Teleport Destinations
TeleportService limits teleportation to places in the same game or to the starting place of another game (assuming the owner has made it public).
When a player is teleported between places, any custom data associated with that player is discarded. Thus, it’s very important that you consider data persistence when using
If players utilize any amount of secure custom data in your game, for example health, in-game currency, complex player stats, and/or a large inventory of items,
/articles/Data store|data storesshould be implemented to maintain data as players teleport from place to place.
If just a small amount of non-secure data should be retained, like game settings and UI theme, the
TeleportServicefunctions accept a teleportData parameter. This can be a table, string, number, or boolean which can then be retrieved in the destination place via
Teleport on Part Touch
In this example,
TeleportService/Teleport|Teleport() is used to teleport players who touch the script’s parent part.
local TeleportService = game:GetService("TeleportService") local placeID_1 = 408502340 local placeID_2 = 408502380 local function onPartTouch(otherPart) local player = game.Players:GetPlayerFromCharacter(otherPart.Parent) if player then TeleportService:Teleport(placeID_1, player) end end script.Parent.Touched:Connect(onPartTouch)
Follow Another Player
The code sample below, when placed inside a
ServerScriptService, will teleport a player who’s following another player to the associated place/server.
local TeleportService = game:GetService("TeleportService") local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) -- Is this player following anyone? local followId = player.FollowUserId -- If so, find out where they are if followId and followId ~= 0 then local success, errorMessage, placeId, jobId = pcall(function() return TeleportService:GetPlayerPlaceInstanceAsync(followId) end) if success then -- Teleport player TeleportService:TeleportToPlaceInstance(placeId, jobId, player) end else warn("Player " .. player.UserId .. " is not following another player!") end end)