Help with teleportservice

for some reason,the code just does not work. This is executed when the player joins, yet when me and my friend test it we still end up in the same lobby. Private servers are enabled, http requests are enabled, and api services are enabled so why does this not work?

local code = ts:ReserveServer(game.PlaceId)
ts:TeleportToPrivateServer(game.PlaceId, code, player)
print(teleport)

I think it’s because your game isn’t public

You should probably use protective calls to use the functions, plus test it in an actual Roblox game.

It is public and still doesn’t work

What are protective calls and I am testing in an actual game

I don’t think you should be learning this service since it involves network calls but

Protective calls are a built-in vanilla Lua function which allows you to run a code in a scope without the errors in it stopping the execution of your code. The function you used in your code: TeleportService:TeleportToPrivateServer() involves network calls in order to get players teleported to another server, and it can occasionally fail. You should probably watch a YouTube tutorial on how to use this service.

pcalls catch errors so that they don’t kill your script.

pcall takes two arguments: a function, and optionally arguments to run them with.

For example:

local success, ret = pcall(print, "Hello, world!")

would print “Hello, world!”.
success would be true, and ret would be nothing (because print doesn’t return anything).

See Lua Globals, and this tutorial by ReturnedTrue.

The player parameter is supposed to be an array of players. You’ll want to make a table and insert the player into that table and pass the table as the player parameter.

2 Likes

Check out this for more information
https://developer.roblox.com/en-us/api-reference/function/TeleportService/TeleportToPrivateServer

Could u perhaps elaborate more?

Would I do (game.PlaceId, code, tablewplayer)

Like that?

The teleport service isn’t super hard to learn, and network calls aren’t the end of the world.

A protective call (also know as pcall) allows your code to handle errors. Normally when an error happens, it ends the code. When wrapped in a pcall like so:

local success, errorMessage = pcall(function()
    error("Oh no! An error!")
end)
if not success then
    warn("There was an error: "..errorMessage)
    -- Put code here to handle the error
else
    -- The code to run if there isn't an error
end

you can handle errors that happen. Functions that use network calls, for example, sometimes run into unsolvable errors, since networks aren’t always reliable. When they do, those functions result in an error. If you don’t handle the error it can break your code.

You problem probably isn’t that you aren’t using a pcall. You should though.


It’s a little weird the provided code works, the players parameter should be a table/array, not a Player. I bet you can fix your code like so:

local code = ts:ReserveServer(game.PlaceId)
ts:TeleportToPrivateServer(game.PlaceId, code, {player})
print("teleport") -- Added quotes around teleport: didn't see a var named teleport.

If the players are going to the same server, the problem also might be that you’re using the same code for all of them. If you want them to go to different servers you’ll need to reserve a code for each individual server.


PS:

You can write code boxes using three backquotes followed by three back quotes:

```

-- Code

```
-- Code

Edit:

I believe you can just have the starting place of the game be a menu, then teleport the players from the menu place to the “public” place. If you do something like that you don’t need to have your players go through more teleports than needed. When you need to send them back you can teleport them normally, since you don’t necessarily need private servers when the max player count is one (to get the place id of the start game you can just store it in a settings table in your game).

Ah ya I know pcalls. Basically, everything a player joins they are at the start menu. I want the player when they join to be telported to a new vip server where it is just them so that they won’t see other players characters until joining the public servers when they click play. Ah dang and I just realized another problem is that when they join back they will be put at the main screen again, god damn this problem is a pain. Thx man for the reply tho

1 Like