Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How might I find a table with a dynamic name?

Asked by
algo160 10
9 years ago

I have a variable, and a string with the variables name. Is it possible for me to find the variable with that name, through code ( dont want to use eval(), unless i have too)

0
You shouldn't usually have this situation. How did this happen? BlueTaslem 18071 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

If you put those variables in a table, then you can get the variable's name through code, like so:

local Var_Table = {
    A = true;
    B = 20;
    C = "Hello World!";
}

print(Var_Table["A"]) --prints "true"
print(Var_Table["B"]) --prints "20"
print(Var_Table["C"]) --prints "Hello World!"

Hope this helped!

Ad
Log in to vote
1
Answered by 9 years ago

You can use the getfenv function to return the current environment in a table.

local String = "MyVariable"
local Variable = getfenv()[String]
if Variable then
    print(Variable)
end

Note that in newer versions of Lua (Which Roblox does not use) you get the environment from the _ENV variable and not getfenv

Answer this question