The output isn't printing out "Hot Dog". I am trying to learn table.find() and I started adding more tables in side of tables to see how it would work, but it just prints out nil, why is that? Here's the code
local t = { {"Burger",1000}, {"Taco",5000}, {"Hot Dog",150000}, {"Steak",60}, {"Grilled Cheese",10} } for i, number in pairs(t) do print(number) print(table.find(t,"Hot Dog",5)) end
The reason it is printing nil is because you are searching the table t for "Hot Dog", however, it only has tables inside of itself. Instead, you should search each sub-table:
local t = { {"Burger",1000}, {"Taco",5000}, {"Hot Dog",150000}, {"Steak",60}, {"Grilled Cheese",10} } for i, tbl in pairs(t) do local number = tbl[2] print(number) print(table.find(tbl,"Hot Dog")) end -- Output: -- 1000 -- nil -- 5000 -- nil -- 150000 -- 1<-- This is where "Hot Dog" is located. -- 60 -- nil -- 10 -- nil
Here is a script I toke from roblox references] https://developer.roblox.com/en-us/api-reference/lua-docs/table
local t = {"a", "b", "c", "d", "e"} print(table.find(t, "d")) --> 4 print(table.find(t, "z")) --> nil, because z is not in the table print(table.find(t, "b", 3)) --> nil, because b appears before index 3
Judging from that script maybe try changing yours to
local t = {"Burger",1000, "Taco",5000,"Hot Dog",150000, "Steak",60, "Grilled Cheese",10} for i, number in pairs(t) do print(number) print(table.find(t,"Hot Dog",5)) end