How to change a variable with a string

hello developers,

So i was wondering if this is possible


--lets say i have some strings.
local table1 = {"A","B","C","D"}


--So i have some variables
local A
local B
local C
local D

for Number, letter in pairs(table1) do
  --letter = A
  --How can i make A = true, because i got A? (automaticly)

  --What i don't want is to write everything myself
  --so i don't want to write:
  if letter == "A" then
     A = true
  end
end

Does everybody understand what i am after? i just want a way to change a variable automaticly, because i am going to add some more letters to table1 and i don’t want my script to me “unreadeble”

You can unpack the table to return its values.

local table1 = {"A","B","C","D"}
local A, B, C, D = unpack(table1)

print(A,B,C,D)
1 Like

oh, i forgot to say that the table is in a modulescript, do you know how to do get it from a module, or a alternative way to do this?

Also thanks for the reply

You simply require the module form a regular script and inside of the module script at the end you write

return table1

i don’t exacly understand, how do i fix this by onyl adding a “return”?

You asked how to get the table from a module script and I told you how to do that. @kingdom5 answered the main question.

ohh, yeah, but kingdom5 didn’t solve the question, and i didn’t understand what you meant. well…

How come he didn’t solve it, that’s the only way you can do that because the script can’t read variable names as strings.

oh, i didn’t knew that.

but wouldend it print A = “A”

i asked for A = true or false

There’s a way which might be what you need:

local letters = {
	["A"] = false,
	["B"] = false,
	["C"] = false,
	["D"] = false,
}

local function reset_letters()
	for k, _ in pairs(letters) do
		letters[k] = false
	end
end

for Number, letter in pairs(table1) do
	--letter = A
	--How can i make A = true, because i got A? (automaticly)

	--What i don't want is to write everything myself
	--so i don't want to write:
	reset_letters()
	letters[letter] = true
end

You can use the table (dictionary) to access these variable instead of making each one its own individual, which would require tons of if statements.

Note: This reset letters function is optional, as I don’t really understand if you wanted to switch the active letter or toggle them.