Search code examples
luatabletop-simulator

lua how to get table within table use variable value rather than variable as an index? TTS


Newbie Programmer I am using table within a table to store information on tabletop simulator using scripting

rpgPlayer = 123456 -- this is a string of values
masterTable.rpgPlayer = { test = "test1"} 
for n in pairs(masterTable) do 
    print(n) --- this comes out as rpgPlayer instead of 123456 
    end  

I used a cut down example of code but basically instead of creating a table within masterTable with the key = 123456 it creates a key = rpgPlayer is the way to point the program at value of rpgPlayer rather than the variable ?


Solution

  • Use bracket syntax. tbl[var]

    Dot syntax (tbl.var) uses string variable as value, this syntax is equivalent to tbl["var"].

    You need to do this:

    masterTable[rpgPlayer] = { test = "test1" }
    

    Read more about table syntax: https://www.lua.org/pil/2.5.html