I can't access OnJoinData

I’ve been trying to pass along data through the TeleportService but It’s doesn’t work for and always returns nil. The general idea of the game is the main place is a Hub world where players group up and teleport to Reserved Servers. Whenever I try to pass each player data via userId, it always returns nil in the reserved server world

Hub/Teleport Script:

local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local RunService = game:GetService("RunService")

local zonePart = workspace.devportal
local destinationPlaceId = 118365563440923



local playerData = {}


local teleOpt = Instance.new("TeleportOptions")
teleOpt.ShouldReserveServer = true


zonePart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then

		teleOpt:SetTeleportData(playerData[player.UserId])
		TeleportService:TeleportAsync(destinationPlaceId, {player}, teleOpt)
	end
end)

Players.PlayerAdded:Connect(function(player)
	-- Store player data when they join
	playerData[player.UserId] = {
		name = player.Name,
		userId = player.UserId
	}
end)

-- Clean up player data when they leave
Players.PlayerRemoving:Connect(function(player)
	playerData[player.UserId] = nil
end)

ReservedServer Script:

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

Players.PlayerAdded:Connect(function(player)
	print("Player arrived:", player.Name)

	local joinData = player:GetJoinData()

	if joinData then
		print("Retrieved join data for", player.Name, ":", joinData)

		if joinData.userId == player.UserId then
			print("Data verified for correct player!")

			-- Use the retrieved data
			if joinData.name then
				print("Player name from data:", joinData.name)
			end

		else
			warn("Data mismatch! Expected UserId:", player.UserId, "Got:", joinData.userId)
		end
	else
		warn("No join data found for player:", player.Name)

	end
end)

Try changing the variable “joinData” to be equal to player:GetJoinData().TeleportData rather than player:GetJoinData().

:GetJoinData() returns more than just the teleportData you set as shown in the docs:

You can get the teleport data by accessing joinData.TeleportData

local joinData = player:GetJoinData()
local tpData = joinData.TeleportData
1 Like

mannn i’ve been stuck looking at the code I had for 2 days. Therefore I decided to just start over . So after getting this error, I gave up and came here. I don’t what my original problem was but you sir just fixed my current one.

thank you so much frfr😂

1 Like

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