hi i'm new with tables
local testtable = {
subtable1 = {"duck","hat","goose"},
subtable2 = {"truck,","fart","house"}
}
for i, v in pairs(testtable) do
print(v["fart"])
end
it prints nil(x2)
hi i'm new with tables
local testtable = {
subtable1 = {"duck","hat","goose"},
subtable2 = {"truck,","fart","house"}
}
for i, v in pairs(testtable) do
print(v["fart"])
end
it prints nil(x2)
When you put v["fart"], you're attempting to check if the index "fart" is within the testtable. That would be the case for a dictionary, so print(testtable["subtable1"]) would work since subtable1 is the key in your dictionary. You have the right idea on using a for loop, but then you'll need another for loop to access the nested tables! Here's how:
local testtable = {
subtable1 = {"duck", "hat", "goose"}, -- subtable1 is key, {"duck", "hat", "goose"} is its value
subtable2 = {"truck", "fart", "house"}
}
for key, value in pairs(testtable) do
for i, v in pairs(value) do
if v == "fart" then
print("Found fart!")
end
end
end
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
nil means it cant find the thing you are trying to print or there is an error while trying to print it
I know what nil means. The title indicates that I do having said "why cant the script find."
any errors?
Don't do ["fart"], just do print