How to find a value in a table

I am having an issue where I want to find a value within a table.
I am trying to use table.find() not to find a value itself but what the value is defining.
For reference I want to find “Common” in this table to find what it equals using a number that it returns:

local Prefixes = {
	Common = "a",
	Uncommon = "an",
	Rare = "a",
	Epic = "an",
	Legendary = "a",
	Exotic = "an"

So what I am trying to do to find it is:

CurrentPrefix = table.find(Prefixes, Common)

and then:


to find the value of the input, but it just returns nil.
This is I think because if Common = "a" then table.find would be searching for "a" rather than Common which is not what I want.
In this case Common is an object value rather than a string.
I want to find the number in the table that Common is at, which is what table.find is supposed to return.

If anybody has solutions please let me know. Also if what I said is too confusing then just ask for clarification and I’ll try to help.

local Prefixes = {
	["Common"] = "a",
	["Uncommon"] = "an",
	["Rare"] = "a",
	["Epic"] = "an",
	["Legendary"] = "a",
	["Exotic"] = "an"

prefix = Prefixes["Common"]

print(prefix) --> "a"

Is this what you’re looking for?


Works great! Thanks, I’m still learning how to use tables so this was great help. :+1:

1 Like