How will I access the values inside a table that is inside another table?

Okay, I have sent the data while teleporting players by doing this:

local ServerCode = game:GetService("TeleportService"):ReserveServer(11132423100)
	local players = {}
	local data = {}
	
	for index, Plr in pairs(game.Players:GetPlayers()) do
		table.insert(players, Plr)
		table.insert(data, Plr.Name) 
		data[Plr.Name] = {
			CharacterValue = Plr.PlayerGui.Play.Frame.CharacterValue.Value,
			PlayerTeam = Plr.PlayerGui.Play.Frame.SelectedTeam.Value,
			PlayerSkin = Plr.PlayerGui.Play.Frame.SelectedSkin.Value
		}
		game:GetService("TeleportService"):TeleportToPrivateServer(11132423100, ServerCode, players, nil, data)
	end

I basically sent: data → playername → the values I want

I want to confirm the data belongs to the right player, then access the values inside it to use them.
I managed to confirm it belongs to the player by checking names, I just need to find a way to access the values inside it.

player = game.Players.LocalPlayer
local tService = game:GetService(“TeleportService”)
local data = tService:GetLocalPlayerTeleportData()
local dataowner = unpack(data)
local playername = player.Name

if data and dataowner == playername then
local CharacterValues = ???
local PlayerTeams = ??? (How will I access these values?)
local SkinValues = ???
script.Parent:FireServer(CharacterValues,PlayerTeams,SkinValues)
end

The way you’ve constructed your table, the keys will be strings of the variable name used:

CharacterValues = data[plr.Name].CharacterValue

It says attempt to index nil with CharacterValue

data doesn’t contain the player name then. Can you try looping through data with a for loop to see whats in it? Something might be getting lost in transmission.

When I say print(unpack(data)), it gives out my name. Meaning it does indeed send my name. I guess it doesn’t have the values in it? If I try to straight up print(data) it just gives a hexcode.

The hex is the address of the table. Try printing out the elements of data like this:

for k, v in pairs(data) do
print(k .." : " ..v) --Print key and value
end

it printed out my name: 1: OyunKlavuzu

My bad, I misread your code.

table.insert(data, Plr.Name) 

Inserts Plr.Name at the next number index. I would recommend doing:

data[Plr.Name] = {
--CharacterValue etc
}

Then the lookup I gave you initially will work as you can index the data table with the player name. This is preferable if the order of players in the Data table does not matter, which it shouldn’t imo.

1 Like

Alright, didn’t know it was such a small mistake lol. My main reason of checking for player name is to avoid a huge issue. The teleporter teleports everyone with their character,skin and teamvalue. It used to apply a single player’s data to almost everyone else, causing the game to go from a 5v5 to 1v9/6v4 etc. This way, I have the player’s data WITH their name in it. The script below now has everything it needs to check the data and use it only if it matches with the player’s name. How would you recommend me to use it? right now it’s just if data then → fireserver(character,skin,team) Sorry, I’ve been dealing with this for the last 2 days, I need a little help…

put these brackets [] round your value then put them in quotation marks then do as @azqjanna suggested in the first post

Oh so, the data will include every player’s name and their values. Then when it does data.[player.Name].CharacterValue, we’re sure it will grab the right player’s data only, right? Should I move the line that teleports people outside the loop?

local ServerCode = game:GetService("TeleportService"):ReserveServer(11132423100)
	local players = {}
	local data = {}
	
	for index, Plr in pairs(game.Players:GetPlayers()) do
		table.insert(players, Plr)
		table.insert(data, Plr.Name) 
		data[Plr.Name] = {
			['CharacterValue'] = Plr.PlayerGui.Play.Frame.CharacterValue.Value,
			['PlayerTeam'] = Plr.PlayerGui.Play.Frame.SelectedTeam.Value,
			['PlayerSkin]' = Plr.PlayerGui.Play.Frame.SelectedSkin.Value
		}
		game:GetService("TeleportService"):TeleportToPrivateServer(11132423100, ServerCode, players, nil, data)
	end

that what i would do

1 Like

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