How to reserve and teleport to private servers

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    First note that my game has multiple places. I am trying to make sure that when a person uses a private server to teleport to another place, they’d be teleported to a reserved server, unique to the specific private server and not a public one.

  2. What is the issue?
    You simply don’t get teleported to a reserved server…

  3. What solutions have you tried so far?
    I’ve tried firstly storing the code of the reserved server as a variable, then recalling it in the parameters of the lua TeleportToPrivateServer() function.

local circuitIDs = {} -- Won't add in the list as it is irrelevant and long.
local circuitNum = script.Parent.Parent.CircuitNumber
local button = script.Parent
local teleportService = game:GetService("TeleportService")
local firstTime = true
local teleportNewServer = Instance.new("TeleportOptions")

teleportNewServer.ShouldReserveServer = true

button.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	script.Parent.Parent.Parent.Teleportation.Visible = true
	if game.PrivateServerId == "" then
		teleportService:Teleport(circuitIDs[circuitNum.Value][2])
	else
		if firstTime then
			code = teleportService:ReserveServer(circuitIDs[circuitNum.Value][2])
			teleportService:TeleportToPrivateServer(circuitIDs[circuitNum.Value][2], code, game.Players.LocalPlayer)
			firstTime = false
		else
			teleportService:TeleportToPrivateServer(circuitIDs[circuitNum.Value][2], code, game.Players.LocalPlayer)
		end
	end
end)
2 Likes

If I remember correctly, DataModel.PrivateServerId is not replicated to the client. You have to handle teleporting on the server. (Or atleast check wether or not the server is a private server from the server and sending that data to the client)

So does that mean that the rest of the code is right but you just need data from serverside?

But also, what’s DataModel exactly? Does it have a purpose?

No the rest of the code is not right because TeleportToPrivateServer() requires a table of players and not a player instance.

So how to I fix that if I only want the player who clicks to be teleported?

{game.Players.LocalPlayer}

The PrivateServerId property is not replicated to the client so if you want to teleport on the client you will need a RemoteFunction to check if the server is a private server and then send the value back to the client but the easier way would just be to use a RemoteEvent and teleport on the server.

Simplest explanation is DataModel = game

So I use a remote event from the server to tell the client whether or not the server is private, and then teleport the client? Okay. I’ll try that…

One last thing though, if I use a server script inside of StarterGui to check if it’s a private server and change a value that the local script can access, would that work?

Something like this:

wait(1)
if game.PrivateServerId ~= "" then
	script.Parent.Value = "Private Server"
else
	script.Parent.Value = "Public Server"
end

And have this from the local script:

local circuitNum = script.Parent.Parent.CircuitNumber
local button = script.Parent
local teleportService = game:GetService("TeleportService")
local teleportNewServer = Instance.new("TeleportOptions")

teleportNewServer.ShouldReserveServer = true

button.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	script.Parent.Parent.Parent.Teleportation.Visible = true
	if script.Parent.ServerType.Value == "Public Server" then
		teleportService:Teleport(circuitIDs[circuitNum.Value][2])
	else
		if circuitIDs[circuitNum.Value][3] == true then
			code, privateServerID = teleportService:ReserveServer(circuitIDs[circuitNum.Value][2])
			teleportNewServer.ReservedServerAccessCode = code
			teleportService:TeleportToPrivateServer(circuitIDs[circuitNum.Value][2], code, {game.Players.LocalPlayer})
			circuitIDs[circuitNum.Value][3] = false
		else
			teleportService:TeleportToPrivateServer(circuitIDs[circuitNum.Value][2], code, {game.Players.LocalPlayer})
		end
	end
end)

Also thank you very much for your help so far.

Just teleport on the server.

Server:

local teleportEvent = -- a RemoteEvent

teleportEvent.OnServerEvent:Connect(function(player) -- the first argument to an OnServerEvent callback is always the player who fired the event
    -- reserve the server and teleport the player
end)

Client:

button.MouseButton1Click:Connect(function()
    -- fire the teleport RemoteEvent
end)

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