Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How might I find a table with a dynamic name?

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)

1 comment

0

You shouldn't usually have this situation. How did this happen?

2 answers

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!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.