Player:GetJoinData().TeleportData.data is nil?

I’m trying to teleport a player with a couple of number values, but the values are nil once the player has teleported?

before the teleport (server)

game.ReplicatedStorage.event.OnServerEvent:Connect(function(value1,value2,value3,value4,value5)
	local teleportOptions = Instance.new("TeleportOptions")
	print(plr,fab,bel,bop,pow,jl)
	local teleportData = {
		placeId = game.PlaceId,
		jobId = game.JobId,
		value1,
		value2,
		value3,
		value4,
		value5
	}
	teleportOptions:SetTeleportData(teleportData)
	game:GetService("TeleportService"):TeleportAsync(8522949336, {plr}, teleportOptions)
end)

after the teleport (server)

if plr:GetJoinData().TeleportData then
	print(plr:GetJoinData().TeleportData.value1)
end

It prints nil instead of the value, no errors or anything.

I think its because the values aren’t set to anything. This defaults them to nil. You should put if value1 ~= nil just incase the data is nil, so your script won’t break.

try entering the table via for loop and printing its contents

i.e.

 for index, value in pairs(plr:GetJoinData().TeleportData) do
print(index .. " and " .. value)
end

nil means nothing exists for the value so something

also shouldnt this

local teleportData = {
		placeId = game.PlaceId,
		jobId = game.JobId,
		value1,
		value2,
		value3,
		value4,
		value5
	}

be

local teleportData = {
		placeId = game.PlaceId,
		jobId = game.JobId,
		value1=value1,
		value2=value2,
		value3=value3,
		value4=value4,
		value5=value5
	}

your current code just says value1 and etc, its not being assigned a value.

It’s nil because it doesn’t exist. Values 1-5 are entered into the array portion of the table which are indexed as 1-5. You’re attempting to access the index “value1” of the TeleportData table which doesn’t exist. value1 lives in the index TeleportData[1].

Consider packing your values into a table instead:

local teleportData = {
    placeId = game.PlaceId,
    jobId = game.JobId,
    values = {value1, value2, ...}
}

Player:GetTeleportData().TeleportData.values[1]

Or following the above advice by turning your teleportData table into a dictionary instead of a mixed table. You typically don’t want to have mixed tables anyway, Roblox APIs are known to be very unkind towards them.

You didn’t pass those values with the event, also, player is always the first argument in OnServerEvent so why don’t I see it?

1 Like