Why does my script print nil, when I try to find a Value inside a table that is in a table?

This script keeps printing nil

for i, Name in pairs(Names_Ids) do

			print(Name[2])
			if table.find(Name,UserId) then
				PlrIndex = Name[3]
			end
		end

Names_Ids is a table that holds players’ and this players have a tables inside of them

PlayersName = {Name = "Name", Id = PlayerId, ClearanceLevel = ??},

This is not fill out but it would have their name PlayerID and Clearance level filled in. But my code above prints nil for some reason, It should print they’re Id but it prints nil

table.find won’t work because you’re actually using dictionaries, which have a custom key for an element. This means you can’t index elements like you would a table as the elements not are indexed by incrementing integers. You could instead get elements with Name["Name"] or Name.PlayerId (both ways of indexing work, but I think the first is more readable).

Also, I think that using a table of dictionaries is over complicating your implementation. You should use a single dictionary instead. Something like this:

local PlayerInfo = { 
	[player.UserId] = { -- Use UserId as the key as they are unique and immutable
		["Name"] = "Name",
		["ClearanceLevel"] = "??"
	};
}

Then you can loop through the dictionary with

for userId, info in pairs(PlayerInfo) do
	print("The clearance level for " .. info["Name"] .. " is " .. info["ClearanceLevel"])
end

And you can easily index any player’s information with

local info = PlayerInfo[player.UserId]
1 Like

I’ve changed to use this

for i, Player_C in pairs(Names_Ids) do
			if Player_C["Id"] then
				if Player_C["Id"] == UserId then
					PlrIndex =Player_C['ClearanceLevel']
				end
			end
		end

It’s not Name[2] or Name[3]. Normally it would be but in this case you have set a key for them so it’s not.

Try this instead

for i, Name in pairs(Names_Ids) do
	print(Name.Id)
	if table.find(Name,UserId) then
		PlrIndex = Name.ClearanceLevel 
	end
end

Are you sure the number 2 is not seen by the computer as a string?

As i have clearly stated this does not work, the table.find() only give nil as an output

Sorry i have already have solved this problem as of being 3 hours ago, but Gnarwhals method could work

o my bad didn’t see that, I just read the original post and find problems with it. Thank you for pointing it out though, I didn’t know table.find() doesn’t work with that.

Ya idk why but it only prints nil for me

I think it’s because UserId is the value in the table and not the index. If the UserId is equal to Name[1] then you have to do table.find(Name,1)

UserId is a long number list of a real player’s ID code. it is not a value in a table