Getting the name of a variable?

The title is really all I need to say. How would I get the name of a variable?

local nameIWantToGet = '<-- that!'

And, would the method you propose work with table variables?

local tableWithNames = {
    nameIWantToGet = '<-- that!',
}
1 Like

I am slightly confused about what the use case of this would be. If you didn’t already know the name of a variable, why are you trying to access it?

you cant unless its a string or number i think

local tableWithNames = {
        "nameIWantToGet" = "whatever",
}

for i, v in next, tableWithNames do
        print(i)
end

That’s the thing, the script wouldn’t be able to get a string version of the name of that variable.

This WILL work if you surround “nameIWantToGet” in brackets, but I’ve seen other scripts get the name without that. I would love to do it with the normal way (without brackets and strings).

It’s impossible to change the name of a variable or get the name of a variable in Lua, the language Roblox uses. There are no keywords for this. While running, the script actually doesn’t even care what the variables are named. It just has the keep them separate from each other and remember which is which.

I do not want to change it- I want to get the name of it as a string.

Then why would this work?

print(tableWithNames.nameIWantToGet) --> "<-- that!"

Nevermind, figured it out.

local Table = {
	Food = "Apple";
}

for i, v in pairs(Table) do
	print(i, v) --> "Food Apple"
end

oh now i know what you mean

you want to get the name of the variable here:

local hi = "hello"

not

local hi = {hi = hello}

Ohhhhhhhh…

That’s not a variable though. That is just the key to that object in the dictionary. Yes, that is very possible.

That’s different from a variable

Well, it works for my second use case (which I was planning to use anyways), so I’m good with it. :slight_smile:

1 Like

Fun fact: you can get the name of a variable with getfenv, but not local variables

N1_G = 'hey'
N3_G = 'nope'
local NoShow = 20

local env = getfenv()
for k, v in pairs(env) do
	if v == "nope" then
		warn(k)
	end
end
print(env)

image
As you can see there, NoShow does not appear in the list.

2 Likes

Roblox uses Luau : (

Lua is a different language, the difference between the 2 is Luau is a bit more closed PC wise, while like any other language Lua can have full file access.

1 Like

luau is a fork of lua, not a different language

not saying it is different as an entirely different language, im saying it is different as in what it is used for

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.