Getting a variable name in table from the value?

Is it possible to get the name of the variable through using the variables value like below?

local Table = {
     Food = "Apple";
}

So if I have “Apple” is there a way to get the name “Food” somehow?

You can use a generic for loop, where key is the name, in this case Food, and value would be the value, in this case "Apple"

for key, value in pairs(Table) do
    -- stuff
end
1 Like

If you’re doing it as a dictionary, you could also do:

local Table = {
    ["Food"] = "Apple"
}

And then access it with:

Table["Food"]

as stated i would use the for i,v in pairs loop

this is how you would do it if you would like me to explain how it works feel free to ask

local Table = {
	Food = "Apple";
}

for i, v in pairs(Table) do
	print(i, v)
	if i == "Food" then
		print(v, "is a food")
	end
end

output:image

As pointed out by incapaz and S0MBRX, in your case do

for itemType, name in pairs(Table) do
    if name == “Apple” then
        print(name..”is a “..itemType)
    end
end

It doesn’t work guys please help i also need help