So say you have a Dictionary like this:
local Dictionary = { ['Index1'] = {PartObjectValue,PartObjectValue,PartObjectValue}, ['Index2'] = {PartObjectValue,PartObjectValue,PartObjectValue}, ['Index3'] = {PartObjectValue,PartObjectValue,PartObjectValue} }
How would i set a variable to only the string part of, for example, 'Index2' and not set the variable to what Index2 equals? Example:
local Var = Dictionary[1] -- <--- ???? This will only set it to whatever Dictionary[1] equals and not its name
Well, for one Dictonary[1] in your case would happen to be nil, because the value at 1 is not defined.
However, to obtain a key based on the value (Do note it has to be the exact value, something similar won't count.), you can use an approach like the following:
local FindFirstKey=function(Table,Value) for Key,Val in pairs(Table) do if Val==Value then return Key end end --Returns nil by default. end Dictionary = { ['Index1'] = "Hello", ['Index2'] = "Goodbye", ['Index3'] = {PartObjectValue,PartObjectValue,PartObjectValue} } local Key=FindFirstKey("Hello") print(Key) --> "Index1" --HOWEVER this approach only works for exact values. Key=FindFirstKey({PartObjectValue,PartObjectValue,PartObjectValue}) print(Key) --> nil
This occurs because you're making a new table, which technically isn't in the original!
However, this is worked around by actually passing the table that does exist in the dictionary
local RealTable=Dictionary["Index3"] print(FindFirstKey(RealTable)) --> "Index3"
Then again, I doubt you have any actual use case for that.
TL;DR: A simple pairs loop should do the trick.
Your indexing it like it's an array. But, its a dictionary. When we think of dictionaries we need to think of key = value pairs. Meaning that a key returns a value. The best way to index a dictionary is this.
--Array Example-- Array = {1,2,3} print(Array[1]) --> Output = 1 -- Since Lua Indexing Starts At 1. Unlike, Other Languages Such As Python. --Dictionary Example-- Dictionary = { ['key1'] = 'value1' ['key2'] = 'value2' ['key3'] = 'value3' } print(Dictionary.key1) --> Output = value1 -- Since The Key Returns The Value --How You Should Index Your Example-- --Unlike the example above, you have an array in your value. So, this is a special case, but since we know how to index an array and a dictionary, we can combine these techniques, to index this DictionaryArray! DictionaryArray = { ['key1'] = {1,2,3}, ['key2'] = {4,5,6}, ['key3'] = {7,8,9} } print(DictionaryArray.key1[1]) --> Output = 1
Also, eternal_silverfox's method works as well. But, should only be used if you don't know what key your looking for.
Hope This Helped!
More On Lua Here: https://scriptinghelpers.org/guides/lua-quick-start-guide