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
10 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 — 10y

2 answers

Log in to vote
1
Answered by 10 years ago

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

1local Var_Table = {
2    A = true;
3    B = 20;
4    C = "Hello World!";
5}
6 
7print(Var_Table["A"]) --prints "true"
8print(Var_Table["B"]) --prints "20"
9print(Var_Table["C"]) --prints "Hello World!"

Hope this helped!

Ad
Log in to vote
1
Answered by 10 years ago

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

1local String = "MyVariable"
2local Variable = getfenv()[String]
3if Variable then
4    print(Variable)
5end

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