Is it possible to call a variable like this?

So, I have different button locations example:

ButtonLocation1
ButtonLocation2

which are different variables, if I had a for loop and i was set to an number is it possible to do something like this?

ButtonLocationi
1 Like

Best bet is to make a table of all your buttons then index using that.

local buttons = {
    buttonLocationClose, buttonLocationDrag
}
for _, location in pairs(buttons) do
    -- whatever you want to do with the value from the table index
end

Hmm… not sure how to implement that in my case, certain amount of buttons will appear base on how many buttons they have purchased… and there are 6 buttons so each button is + one button to appear, and they start with 2 how can I make it so it automatically adds the amount with out adding each manual

Assuming you have a parent frame or folder, you can check if a descendant is added then add that descendant to the table of buttons. If it’s removed then you can check if it’s in the table then remove it from the table

But this system is the one that adds it so that won’t work either.

I’m gonna need more information about what system you have in place if I’m to help further

Kind of like this

function enableUI(UI, Pos, Par)
    for i, v in pairs(Pos) do
        local clone = UI:Clone()
        clone.Name = "CreateCharacter"..i.."Button"
        clone.Position = v
        clone.Parent = Par
        clone.Visible = true
    end
end

-- Actual functionality --

local Positions = {Button1Location, Button2Location}
enableUI(CreateCharacterButtonTemplate, Positions, CharacterSelectionMenu)

If I understand correctly, you are trying to fetch the reference of a variable from its string name. I believe you can do this by using getfenv()[NAME_OF_VARIABLE_HERE_AS_A_STRING].

Is the string suppose to be in [] and inside of the ()?

Not sure if I did this wrong but this didn’t seem to work.

table.insert(Positions, getfenv()["Button3Location"])
print(Positions[2])

getfenv will return a dictionary of all the global variables in the current environment. So if I did this:

local a = 10
local b = { 'hi' }

printing getfenv() would give me:

{
    "a": 10,
    "b": { 'hi' }
}

therefore, getfenv()['a'] would give me 10.

Oh wow! Okay thanks! I have only ever seen it in requiring stuff! That’s awesome, thanks a lot, have a great day!

1 Like

Yeah, I think the malicious scripts use it to fetch the require function without explicitly writing it out so it can’t be detected.

You won’t want to use getfenv, as its use will disable some Luau optimizations. Just use a table directly.

5 Likes

This! Please don’t use getfenv. It will undoubtedly lead to horrible, horrible code.

I see, okay thank you for letting me know that

you can do like this,

local Example1, Example2 = "Example One", "Example Two"

instead of creating multiple lines to store something, you can simply do this.

and if you want to refer the variable its simple. example:

print(Example1)

I am saying can I call a variable with another variable, say “i” was equal to 1 can I do Buttoni in any way?

You’re trying to do something pretty strange. When variables are defined as strings, like the dictionary case where

local test_dic = {
	foo = 1,
	fee = 2,
	fii = 3,
}

equals to

local test_dic = {
	["foo"] = 1,
	["fee"] = 2,
	["fii"] = 3,
}

In these cases, you can take the repeating part of the variable and combine strings to index these keys.

local repeating_part = "f"

local test_dic = {
	["foo"] = 1,
	["fee"] = 2,
	["fii"] = 3,
}

print(test_dic[repeating_part.."oo"])
print(test_dic[repeating_part.."ee"])
print(test_dic[repeating_part.."ii"])
Output:
1
2
3

But yeah, directly indexing a local var is not really possible without maybe using obscure and rare luau functions.

psa: please don’t use those obscure functions like getfenv(), using tables is much better, simple and beautiful for everybody, there’s no need to overcomplicate things.

1 Like