How to get variable from string

Hello, I need to do one of the following

A:

Find a variable in a script based on a number. For example:

local b1
local b2
local b3

b..2 = test

B:

Create a way for a variable to act as a reference to another. For example:

local original
local referenceToOriginal (somthing here or whatnot) original
referenceToOriginal = 7
print(original)  ->  7
-- This is an example Lua code block

What would be the best way to go about this?

2 Likes

I don’t 100% understand what you’re trying to do exactly. But I think what you want to do is create a table with your variables assigned to a key you can fetch whenever.

local = Variables = { } --blank table

if not Variables["b2"] then --if the index doesn't exist then you can assign it
 Variables["b2"] = test
end

If this doesn’t answer your question or you are a bit confused feel free to let me know!

1 Like

Yea the best way to modify/reference variables would be from a table, since you can not assign to a variable from another variable or concatenate a variable name, but in a table you can concatenate for the key to reference a value in the table.

local testTable = { b2 = "hello" }
local b = "b"
local two = 2
print(testTable[b..two]) --> hello

Yep I come from OOP (Java, C#) so this was a new concept to me and I was scratching my head for a bit there, thanks for the help!

1 Like

That’s because fields are automatically stored as string values unless otherwise specified.