How can I teleport a specific person to teleport to a Private Server?

I am trying to teleport a player to a private server, in the Roblox Wiki, it uses GetPlayers() which get everybody in the server to teleport. I only want to get a specific person to teleport to a private server, it saids that

Argument 3 missing or nil

Here is my code:

local TS = game:GetService("TeleportService")

local code = TS:ReserveServer(6641341104)

script.Parent.MouseButton1Click:Connect(function(Player)
	TS:TeleportToPrivateServer(6641341104,code,Player)
end)

This is in a Server Script.

2 Likes

The player is not sent into the function from the MouseButton1Click event – since this is being done from a LocalScript, the LocalPlayer can be referenced separately:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local TS = game:GetService("TeleportService")

local code = TS:ReserveServer(6641341104)

script.Parent.MouseButton1Click:Connect(function()
	TS:TeleportToPrivateServer(6641341104, code, player)
end)
1 Like

Sorry for not being clear, but TeleportService can only work in Server Scripts, that is a server script so I cannot reference the player through LocalPlayer.

1 Like

It actually took me so long to notice this until now aha,

The third parameter of TeleportToPrivateServer is actually an array of players, so you’d need to replace that with:

	TS:TeleportToPrivateServer(6641341104,code,{Player})

Also MouseButton1Click does not have any parameters, get the LocalPlayer instead if this is on a local script:

If you’re trying to do this on a Server Script in a GuiObject, do please change it to a Local Script

local TS = game:GetService("TeleportService")
local Player = game.Players.LocalPlayer
local code = TS:ReserveServer(6641341104)

script.Parent.MouseButton1Click:Connect(function()
	TS:TeleportToPrivateServer(6641341104,code, {Player})
end)
1 Like

Ah I haven’t used the TeleportService much so I was unaware of this – however, since you are listening for user input on a GuiObject, this should be done via a LocalScript before communicating with the server that the player should be teleported.

2 Likes

It only took me 2 years to finally understand how that function worked! Definitely not internally screaming

And yeah, here’s the reference:

2 Likes

TeleportService only works on Server Scripts, so I cannot use LocalPlayer. I am trying to see if I can teleport the player through RemoteEvents.

Well, what you could do is create one inside ReplicatedStorage, reference it from both the client & server script, and see if that’ll work? You’ll need to use FireServer() & OnServerEvent()

Adding a bit more onto our (Now changed) LocalScript:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	Event:FireServer()
end)

And our Server Script inside ServerScriptService:

local TS = game:GetService("TeleportService")
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(Player)
    local code = TS:ReserveServer(6641341104)

    TS:TeleportToPrivateServer(6641341104, code, {Player})
end)

It should look something like this hopefully?

5 Likes

Wait, what?

I use teleport through local scripts…

1 Like

Thank you! That was what I was thinking of but I forgot the {Player} so I was quite confused for a second.

1 Like

Witness my amazing straight line
https://developer.roblox.com/en-us/api-reference/function/TeleportService/TeleportToPrivateServer

3 Likes