Hide the Teleport Failed error ui

When teleporting a player failed this ugly error ui pops up is there any way for it to fail and not for it to pop up? Because I’m using the teleport as a way to check if the server is is still up.

Is there a better way of checking if a server still exists with a job id and without using messaging service?

1 Like

Once you tell the player to teleport, there is no way of preventing the UI from appearing if it doesn’t work.

A method I use to check if a server exists is to use MemoryStore:

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

local serverList = MemoryStoreService:GetSortedMap("serverList")

-- adds this server to the list with an expiry of 10 seconds
local function updateServer()
    local jobId = game.JobId

    local data = {
        jobId = jobId,
        lastUpdate = os.time(),
        maxPlayers = game.MaxPlayers,
        playerCount = #game.Players:GetPlayers(),
    }

    serverList:SetAsync(jobId, data, 10)
end

local function getAllServers()
    local success, data = pcall(function()
        local keyValuePairs = serverList:GetRangeAsync(Enum.SortDirection.Ascending, 100)
        local servers = {}

        for _, pair in pairs(keyValuePairs) do
            servers[pair.key] = pair.value
        end

        return servers
    end)

    if success then
        return data
    else
        warn(`Failed to get server list: {data}`)
    end

    return {}
end

local function isServerAlive(jobId)
    local success, data = pcall(function()
        return serverList:GetAsync(jobId)
    end)

    if success then
        return data
    else
        warn(`Failed to get server data for {jobId}: {data}`)
    end

    return false
end

local function teleportPlayers(players, jobId)
    if not isServerAlive(jobId) then
        return "Server dead"
    end

    local success, message = pcall(function()
        local TeleportOptions = Instance.new("TeleportOptions")
        TeleportOptions.ServerInstanceId = jobId

        TeleportService:TeleportAsync(game.PlaceId, players, TeleportOptions)
        return true
    end)

    if not success then
        warn(`Failed to teleport players: {message}`)
        return "Failed to teleport players"
    end

    return message
end

-- setup the server update loop
task.spawn(function()
    while true do
        updateServer()
        task.wait(7.5) -- 2.5 second margin for error
    end
end)

-- expose the teleport function
shared.teleportPlayers = teleportPlayers

-- Exmaple usage: teleporting players to a random server after saying "lol" 
local function playerAdded(player)
    player.Chatted:Connect(function(message)
        if message == "lol" then
            local servers = getAllServers()
            local randomServer = servers[math.random(1, #servers)]

            teleportPlayers({player}, randomServer.jobId)
        end
    end)
end

Players.PlayerAdded:Connect(playerAdded)
2 Likes

Will this work across places under the same experience? example: I have a serverlist starting place with the maingame under it. Can they share that memory store or no. If so this is really cool and I will try it out.

I believe sorted maps are shared between places in the same experience, yes

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.