Search code examples
luaroblox

How do you teleport to a private server using TeleportPartyAsync() in ROBLOX?


I wanted it to teleport to a private server but it wont teleport and it doesn't show any errors.

Here's the code:

local TeleportService = game:GetService("TeleportService")
local Players = {}
local GamePlayers = game:GetService("Players")
local IsTeleporting = false
local PlayersAllowed = script.Parent.Lobby.Teleporter.MaxPlayers

local function Teleport()
    if #Players > 0 then
        local TeleportPlayers = {}

        for i = 1, #Players do
            local I = i
            if game.Players:FindFirstChild(Players[i]) then
                table.insert(TeleportPlayers, GamePlayers:FindFirstChild(Players[i]))
                TransitionEvent:FireClient(GamePlayers:FindFirstChild(Players[i]))
            else
                table.remove(Players, i)
            end
        end
        wait(0.5)
        IsTeleporting = true
        pcall(function()
            TeleportService:TeleportPartyAsync(TeleportID, TeleportPlayers)
        end)
        
        
        wait(0.5)
        IsTeleporting = false
    end
end

Any help would be appreciated!


Solution

  • it doesn't show any errors.

    pcall(function()
       print("function called")
       error("an error occurred!")
       print("this will not be reached")
    end)
    

    This will print "function called". Nothing else. No error shown.

    From the Lua 5.4 Reference Manual:

    pcall (f [, arg1, ···])

    Calls the function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error object. Note that errors caught by pcall do not call a message handler.

    Check pcall's return values to see if the function ran successfully.

    Compare your code to Roblox's example:

    local Players = game:GetService("Players")
    local TeleportService = game:GetService("TeleportService")
     
    local placeId = 0 -- replace
    local playerList = Players:GetPlayers()
     
    local success, result = pcall(function()
        return TeleportService:TeleportPartyAsync(placeId, playerList)
    end)
     
    if success then
        local jobId = result
        print("Players teleported to "..jobId)
    else
        warn(result)
    end