We use cookies on this site to enhance your user experience

Reattempt Failed Teleport

Reattempt Failed Teleport

Sep 06 2018, 2:40 AM PST

This code sample includes a function that will reattempt a teleport if Roblox servers are busy or an unexpected failure has occurred. It should be ran on the client.

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

local localPlayer = Players.LocalPlayer

local function localTeleportWithRetry(placeId, retryTime)
    local connection
    connection = TeleportService.TeleportInitFailed:Connect(function(player, teleportResult, errorMessage)
        if player == localPlayer then
            print("Teleport failed, TeleportResult: "..teleportResult.Name)
            -- check the teleportResult to ensure it is appropriate to retry
            if teleportResult == Enum.TeleportResult.Failiure or teleportResult == Enum.TeleportResult.Flooded then
                -- disconnect the connection
                connection:Disconnect()
                -- retry in retryTime seconds
                delay(retryTime, function()
                    print("Reattempting teleport")
                    TeleportService:Teleport(placeId)
                end)
            end
        end
    end)
    TeleportService:Teleport(placeId)
end

-- usage example:
local placeId = 0 -- Crossroads
localTeleportWithRetry(placeId, 5)