Help sending data with TeleportService!

I’m working on a lobby system currently, It works for the most part, but when I try to send data when teleporting it doesn’t work. I’m not very familiar with the TeleportService so it could be a simple mistake. Here is the script to teleport the players to the game. It’s a server script fired from a remote event.

local TeleportService = game:GetService('TeleportService')

local lobbyevents = game.ReplicatedStorage.RemoteEvents.LobbyEvents
local sendplayerstogameevent = lobbyevents.SendPlayersToGame


local function Teleportplrs(lobbyowner, placeid, plrstotp)
	
	print(lobbyowner, placeid, plrstotp)
	
	local TpPlace = TeleportService:ReserveServer(placeid)
	
	for i, plrtotp in plrstotp do
		
		TeleportService:TeleportToPrivateServer(placeid, TpPlace, plrtotp)
		TeleportService:SetTeleportSetting("PlayerCharacter", "Bigfoot")
		
	end
	
end


sendplayerstogameevent.OnServerEvent:Connect(Teleportplrs)

Here is the local script inside of starterplayerscripts in the actual game.

local teleportService = game:GetService("TeleportService")

local plr = game.Players.LocalPlayer

task.wait(5)

local teleportData = teleportService:GetLocalPlayerTeleportData()
	
print(teleportData)

When playing it prints nil. Here is a clip of what it does when playing.

  • SetTeleportSetting() is client-only - calling it from the server won’t attach anything to the teleport packet
  • TeleportToPrivateServer() already has a teleportData argument, so you can pass a single table for all players once, rather than looping each player

However, another thing to note is that you can send data way easier using the newer TeleportAsync API instead

TeleportAsync replaces all the older helpers (Teleport, TeleportToPrivateServer, etc), and it has a built in data pipe TeleportOptions:SetTeleportData() that every teleported player can read on arrival

You’ll wanna do something like this instead
Server script:

local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local lobbyEvents = ReplicatedStorage.RemoteEvents.LobbyEvents
local sendPlayers = lobbyEvents.SendPlayersToGame

local function teleportPlayers(sender, lobbyOwner, placeId, players)
    local code = TeleportService:ReserveServer(placeId)

    local options = Instance.new("TeleportOptions")
    options.ReservedServerAccessCode = code
    options:SetTeleportData({ character = 'Bigfoot', owner = lobbyOwner.UserId })

    TeleportService:TeleportAsync(placeId, players, options)
end

sendPlayers.OnServerEvent:Connect(teleportPlayers)

Local script:

local data = game:GetService("TeleportService"):GetLocalPlayerTeleportData()
print(data) -- { character = "Bigfoot", owner = 123456 }

Lmk if that works out better

2 Likes

Ok thank you! I’ll try this tomorrow and let you know how this works. The only reason I looped through each player was to send separate plr data.

1 Like