如果您想建立許多不同地點的體驗,例如擁有多個城鎮、城堡、地牢和廣闊森林的幻想世界,您可以使用 TeleportService 來啟用用戶在宇宙、伺服器或甚至另一個體驗之間進行傳送。
設定傳送
要啟用體驗中的傳送,請使用 TeleportService:TeleportAsync() 。方法接受三個參數:
- 用戶可以傳送到的 PlaceId
- 包含用於傳送的用戶代表的 Player 個實例的數組。
- 可選擇的 TeleportOptions 實例,包含 TeleportAsync() 呼叫的自訂屬性。
local Players = game:GetService("Players")local TeleportService = game:GetService("TeleportService")local TARGET_PLACE_ID = 1234 -- 替換為自己的地點IDlocal playerToTeleport = Players:GetPlayers()[1] -- 獲得體驗中的第一位使用者TeleportService:TeleportAsync(TARGET_PLACE_ID, {playerToTeleport}, teleportOptions)
如果您想在設置傳送時采取錯誤處理的措施,請參見如何處理失敗的傳送。
啟用跨經驗傳送
為了安全目的,將使用者從你的體驗傳送到其他人擁有的體驗或相反,會預設失敗。要啟用跨體驗傳送,請開啟 遊戲設定 > 安全 > 允許第三方傳送 在 Studio 上。
創建自訂傳送畫面
當使用者啟動傳送時,他們會看到標準的 Roblox 載入屏幕,等待新地點載入時。您可以通過呼叫 TeleportService:SetTeleportGui() 在客戶端上增加自定義傳送屏幕來提高用戶沉浸度,然後通過 ScreenGui 在傳送用戶之前使用。以下示例設置位於 ReplicatedStorage 中的自定義 ScreenGui 作為傳送發生時的載入屏幕。它不會在 ScreenGui 內執行任何腳本。
local TeleportService = game:GetService("TeleportService")local ReplicatedStorage = game:GetService("ReplicatedStorage")local teleportGui = ReplicatedStorage.TeleportGuiTeleportService:SetTeleportGui(teleportGui)
自訂傳送選項
您可以通過設置 傳送用戶到特定伺服器 和 傳送用戶數據與傳送 來自定義傳送,例如傳送用戶到特定伺服器和傳送用戶數據一起。
傳送到特定伺服器
若要將使用者傳送到特定伺服器,請使用 TeleportOptions 設定目標伺服器,並傳送到 TeleportService:TeleportAsync() 方法。如果您未指定伺服器,使用者將被傳送到匹配的公共伺服器。列表中第一位使用者的資訊用於匹配到該公共伺服器。
若要將使用者傳送到特定的公共伺服器,請設定 TeleportOptions.ServerInstanceId 屬性為有效的伺服器ID,這是公共伺服器的獨一標示符。
local teleportOptions = Instance.new("TeleportOptions")teleportOptions.ServerInstanceId = targetServerId
若要將使用者傳送到特定保留伺服器,請設定一個有效的 TeleportOptions.ReservedServerAccessCode,這是進入保留伺服器的獨特代碼。
local teleportOptions = Instance.new("TeleportOptions")teleportOptions.ReservedServerAccessCode = reservedServerCode
要將使用者傳送到新保留伺服器,將 TeleportOptions.ShouldReserveServer 設為真實。
local teleportOptions = Instance.new("TeleportOptions")teleportOptions.ShouldReserveServer = true
與傳送一起發送使用者資料
將用戶傳送到不同地點會刪除與該用戶相關的本地資料。您可以使用以下方法來處理地點之間的數據持久性。
若要將基本 非安全 資料從一個地方傳送到另一個地空間,請在傳送前呼叫 。
local teleportData = {randomNumber = RNG:NextInteger(1, 100),}local teleportOptions = Instance.new("TeleportOptions")teleportOptions:SetTeleportData(teleportData)
若要從伺服器上的傳送點接收使用者所有資料,請使用 Player:GetJoinData() 功能,該功能會返回包含使用者相關資料的辭典。
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)
若要只從客戶端中恢復傳送資料,您可以使用 TeleportService:GetLocalPlayerTeleportData() 。
處理失敗的傳送
與任何涉及網路請求的 API 呼叫一樣,傳送可能會失敗並發出錯誤。將它們包裝在受保護的呼叫中(pcall())。某些故障會受到重試的好處,特別是涉及保留伺服器的故障,因此我們建議在故障上重試一定數量的次數。
即使呼叫成功且傳送啟動,在最後一刻仍然可能失敗,而不會發出錯誤並將使用者留在伺服器上。當發生這種情況時,會觸發 TeleportService.TeleportInitFailed 事件。
下面的例子 ModuleScript 定義了一個 SafeTeleport 功能來在保護的呼叫中傳送用戶以進行重試邏輯。它也有 handleFailedTeleport 功能來處理在呼叫成功但傳送未發生的情況。
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 -- 在循環之外定義 pcall 結果,以便稍後可以報告結果
repeat
success, result = pcall(function()
return TeleportService:TeleportAsync(placeId, players, options) -- 將使用者傳送到受保護的呼叫中,以防發生錯誤
end)
attemptIndex += 1
if not success then
task.wait(RETRY_DELAY)
end
until success or attemptIndex == ATTEMPT_LIMIT -- 如果呼叫成功或已達到重試限制,停止嘗試傳送
if not success then
warn(result) -- 列印故障原因輸出
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
-- 如果傳送無效,請舉報錯誤,而不是重試
error(("Invalid teleport [%s]: %s"):format(teleportResult.Name, errorMessage))
end
SafeTeleport(targetPlaceId, {player}, teleportOptions)
end
TeleportService.TeleportInitFailed:Connect(handleFailedTeleport)
return SafeTeleport
SafeTeleport 功能會收到與 TeleportAsync() 功能相同的參數。您可以使用以下 ModuleScript 函數與 SafeTeleport 功能從體驗中的任何地點執行傳送,以減少失敗的傳送。
local Players = game:GetService("Players")local ServerScriptService = game:GetService("ServerScriptService")local SafeTeleport = require(ServerScriptService.SafeTeleport)local TARGET_PLACE_ID = 1818 -- 替換為自己的地點IDlocal playerToTeleport = Players:GetPlayers()[1] -- 獲得遊戲中的第一位使用者SafeTeleport(TARGET_PLACE_ID, {playerToTeleport}, teleportOptions)