I was wondering if there was any way to reference a variable with a string, aside from having the string define a value in an array. Additionally, I want to be able to do it without roblox-specific methods, so no "FindFirstChild()" either. Any ideas?
It sounds like you might be using Lua 5.2 (which is not Roblox). In that case, your solution would look like this:
test=5 print(_ENV["test"])
which you can test here.
otherwise adark's Lua 5.1 solution will work in Roblox.
As long as the variable is not declared using the keyword local
. You can use getfenv
to get a table of all the global variables in the current script:
var1 = 23 local var2 = 57 print(getfenv()["var1"]) -- 23 print(getfenv()["var2"]) -- nil