Failed Teleport attempts multiple times

Here is the suggested script to handle failed teleports and retry

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

In SafeTeleport, it already uses repeat ... until to handle failed teleports. the increment of attemptIndex also happens here.
Then handleFailedTeleport is connected to TeleportInitFailed. It will also call into SafeTeleport. To my understanding, isn’t it would be duplicated teleporting with the repeat ... until in the first level SafeTeleport?

Page URL: https://create.roblox.com/docs/projects/teleporting#handling-failed-teleports

1 Like


Clients are still failling to teleport somehow, after multiple attempt i extablished a pcall course to loop teleport.

TeleportService:TeleportAsync() may just fail for invalid input data, for example, no players specified or conflicting options; in such cases, it’ll return an error, which will be caught and retried in the repeat block but won’t fire a TeleportInitFailed event.
In the case all the arguments are correct and the function call actually returns success, the teleport can still fail due to communications with the backend, in that case it’ll fire the TeleportInitFailed event and should be retried with the SafeTeleport() again.

2 Likes

thank you for clarifying this. I feel like this answer should be included into the documentation

1 Like

Hey EtNowis, I tweaked the explanation to the code sample to better reflect that there are two potential failure points, so retrying in the first loop can sometimes help.

1 Like