Having Trouble with Tables

So I have a table like this:

local petColors = {
	["Cat"] = "FFFFFF",
	["Dog"] = "abffa5",
	["Parrot"] = "52e8ff",
}

And if I want to find something in that table:

local Color = table.find(petColors, "Cat")
print(Color) -- Output is nil
script.Parent.TextColor3 = Color3.fromHex(Color)

Why am I getting nil?

local petColors = {
	["Cat"] = "FFFFFF",
	["Dog"] = "abffa5",
	["Parrot"] = "52e8ff",
}

This is a type of table called a dictionary

I think the table methods only work on arrays
Arrays and dictionaries are different because dictionaries have strings as keys and arrays have positive integers as keys

--array
local arrayExample = {
 "abc",
  123,
 "test",
}

--dictionary
local dictionaryExample = {
  thisIsATest = "abc",
  example = 432424,
}

You can get the value using the key in dictionaries like this:

local dictionaryExample = {
  ["key"] = "test",
  ["anotherKey"] = 123,
}

print(dictionaryExample["key"]) -- outputs "test"
print(dictionaryExample["anotherKey"]) -- outputs 123
2 Likes

Thank you for your help and your explanation! <3

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.