Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why cant the script find this string in the table?

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)

4 comments

0

nil means it cant find the thing you are trying to print or there is an error while trying to print it

0

I know what nil means. The title indicates that I do having said "why cant the script find."

0

any errors?

0

Don't do ["fart"], just do print

1 answer

✓ Accepted answer

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.