Indexing a table returns nil

I want to compare 2 values from 2 tables. Here is an example of it, it returns nil.

local MyTable = {["SubTable1"] = {Bruh = 100}}

local MyTable2 = {Check = {Bruh = 10}}

print(MyTable.SubTable1[MyTable2.Check]) -- prints nil

I basically want to compare the 2 “Bruh Values”. Hope this makes sense.

1 Like

Try using this. Didn’t try it, though.

local MyTable = {["SubTable1"] = {Bruh = 100}}

local MyTable2 = {Check = {Bruh = 10}}

print(MyTable["SubTable1"], MyTable2.Check)
1 Like

Not at all what I need it to do. MyTable2 could be a random check. It might not necessarily be “Bruh”. So thats why I need to find it. Im trying to do this basically

local MyTable = {"A", "B", "C"}

print(MyTable["A"])

But the MyTable2 changes. Don’t really know any other way to explain it.

1 Like

you need to use a for in pair loop to compare the tables

local MyTable = {Bruh = 100}
local MyTable2 = {Bruh = 10}
local Table1Bruh = 0
local Table2Bruh = 0
print("You have:")

for itemName, itemValue in pairs(MyTable) do
	if itemName == "Bruh" then
		Table1Bruh = itemValue
	end
end
for itemName, itemValue in pairs(MyTable2) do
	if itemName == "Bruh" then
		Table2Bruh = itemValue
	end
end

print(Table1Bruh)
print(Table2Bruh)

Just realized you have a sub table. This code will only work on a regular table…

1 Like

I know I do. But it returns nil.

1 Like

Oh, now I understand. At least, I think I understand. Try using this:

local MyTable = {["SubTable1"] = {Bruh = 100}}

local MyTable2 = {Check = {Bruh = 10}}

print(MyTable.["SubTable1"][MyTable2.Check])

When trying this it returns nil hence why I need help.

1 Like

Let me explain this better.


local Table = {Value1 = {Sweets = 10}, Value2 = {Chocolate = 1}, Value3 = {Lolipops = 10}}

local function GetItem(item)
      for  _, v in Table do
            if Item.Requirements == v then
                  print ("Found")
                  -- Here I then want to compare the 2 numbers to see if they are equal 
            end
      end
end)

GetItem({Requirements = {Chocolate = 10}})

It might not be chocolate every time. It might be sweets, or lollipops. But it could be random, that’s why I need to index it.

1 Like

I don’t have much experience with indexing sub tables but I found this might help

Doesn’t work for me, don’t know if it’s me or not.

This should work for you too.

local Table = {{"Sweets", 15}, {"Chocolate", 10}, {"Lolipop", 10}}

local function GetItem(item)
	for  _, v in pairs(Table) do
		for _, vx in pairs(item.Requirements) do
			if vx[1] == v[1] then
				if vx[2] == v[2] then
					print("Found!", v[1])
				end
			end 
		end
	end
end

GetItem({Requirements = {{"Sweets", 15}, {"Chocolate", 10}}})
1 Like

Would this work if instead of a comma we had an =. If not then that won’t work as I can’t change it to commas

I think that should work, tried looking some things up.
If not I don’t know what the problem may be.

local Table = {
	["Sweets"] = 10,
	["Chocolate"] = 10,
	["Lolipops"] = 10
}

local function GetItem(item)
	for namex, v in pairs(Table) do
		for name, itemRequirement in pairs(item.Requirements) do
			if name == namex then
				if v >= itemRequirement then
					print("Found!", name, itemRequirement)
				end
			end
		end
	end
end

GetItem({Requirements = {
	["Chocolate"] = 11,
	["Sweets"] = 10
}})
1 Like
local Table = {Value1 = {Sweets = 10}, Value2 = {Chocolate = 1}, Value3 = {Lolipops = 10}}

local function GetItem(item)
	
	for req_name, req_value in pairs(item.Requirements) do
		for _,v in pairs(Table) do
			if v[req_name] then
				print ("Found")
				if v[req_name]==req_value then print("numbers are equal")
				else print ("numbers are NOT equal") end
				break
			end
		end
	end
	
end

GetItem({Requirements = {Chocolate = 10}})

Appreciate it. Thanks for your help

2 Likes