Need help with teleporting to specific server

so i have a server system which shows all servers in a gui and when you click join on the server it should teleport you to that specific server,

there is a lobby game (where these scripts take place) and the Main game (where player should be teleported to)

however i am confused on how i could change my code to do that, i know to utilize JobId but i dont see where to use it

there is a server script which handles the data, a folder of the servers in repstorage, a local script which handles teleporting and gui

right now what the code does is teleport to a server in the LobbyGame, when it should teleported to a server in the MainGame
server script:

local serversFolder = game.ReplicatedStorage:WaitForChild("Servers")

local ms = game:GetService("MessagingService")


ms:SubscribeAsync("ServerList", function(data)
	data = data.Data
	
	--if data.serverId == game.JobId then
		local serverValue = script.ServerName:Clone()

		serverValue.Name = "Server" .. #serversFolder:GetChildren() + 1
		
		
		serverValue.Value = data.serverId .. " " .. data.players

		serverValue.Parent = serversFolder
		
		task.wait(5)
		serverValue:Destroy()
	--end
end)


while true do
	local data = {
		serverId = game.JobId,
		players = #game.Players:GetPlayers()
	}
	
	ms:PublishAsync("ServerList", data)
	task.wait(5)
end

local script:


local function updateGui()
	
	local serverFrames = {}
	
	for i, serverValue in pairs(game.ReplicatedStorage.Servers:GetChildren()) do
		
		local name = serverValue.Name
		
		local serverStats = string.split(serverValue.Value, " ")
		
		local id = serverStats[1]
		local plrs = serverStats[2]
		
		
		local serverFrame = script.ServerTemplate:Clone()
		
		serverFrame:WaitForChild("ServerName").Text = name .. "\n ID: " .. id
		serverFrame:WaitForChild("Players").Text = plrs .. "/" .. game.Players.MaxPlayers
		
		
		table.insert(serverFrames, serverFrame)
		
		-- If so, find out where they are

		serverFrame.JoinButton.MouseButton1Click:Connect(function()
				print(game.PlaceId)
				print(id)
				
				game:GetService("TeleportService"):TeleportToPlaceInstance(game.PlaceId, id) --11363146366(this is the id to the game they should be tp too)
		end)

		
		script.Parent.List:ClearAllChildren()

		script.UIListLayout:Clone().Parent = script.Parent.List
		
		
		for i, serverFrame in pairs(serverFrames) do
			
			serverFrame.Parent = script.Parent.List
		end
	end
end


updateGui()

game.ReplicatedStorage.Servers.ChildAdded:Connect(updateGui)
game.ReplicatedStorage.Servers.ChildRemoved:Connect(updateGui)

You cannot teleport from the client. The teleport must happen from the server. The function that you are using, TeleportToPlaceInstance has been depreciated. Use TeleportAsync instead.

TeleportService

There’s a link in there to an article that explains the new system better.

I would recommend using TeleportAsync instead of TeleportToPlaceInstance

Although at a glance I think that replacing game.PlaceId with 11363146366 in the line below should solve your issue

game:GetService("TeleportService"):TeleportToPlaceInstance(game.PlaceId, id)

if i were to use TeleportAsync, how would i teleport the the specific server

You would use TeleportOptions to specify the JobId of the specific server that you want to teleport players to. To get the JobId of the destination server, you would need to wait for an event indicating that the teleport was completed. After that, you use the list of UserIds to query what place and JobId the players are at. The query function is GetPlayerPlaceInstanceAsync and is part of the TeleportService.

3 Likes

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