At the moment I have a dictionary. I need to grab the key from the dictionary. But I have a function for that already.
Shown here:
function ReturnTableKeyName(tbl, val)
--Returns the key in a table.
print(blocked)
for k, v in pairs(tbl) do
if v == val then
return k
end
end
return nil
end
The problem though, is if multiple keys have the same value it will return only one of them.
For example:
local d = {
one = 1,
two = 1,
three = 1,
}
--pretend this is a for loop
for blah blah blah do
print(ReturnTableKeyName(d, 1)) --This would always return only one of these, not all three.
end
I’m not sure what you’re trying to do here. Are you trying to get the key or the value?
Otherwise to get a specific value of a key in a table this should suffice. tbl[key]
To get all keys with a specific value you’d probably have to loop through the entire table and return an array of keys, instead of returning the first key you find with that value, as that will always return one key.
you would just make a table of the answers, like this:
function ReturnTableKeyName(tbl, val)
--Returns the key in a table.
print(blocked)
local keytable = {}
for k, v in pairs(tbl) do
if v == val then
table.insert(keytable, k)
end
end
if #keytable > 0 then
return keytable
else
return nil
end
end