I wasn’t too sure what to title this post, so I’ll try to explain what I’m asking.
Is there a way to call a variable in a similar way to this?
local var1 = "1"
local var2 = "2"
local var3 = "3"
for i = 1, 3 do
print(var..i) -- Obviously, this doesn't work
end
put them on a table like this
local Vars = {
var1 = "1",
var2 = "2",
var3 = "3"
}
then loop them like this
for VariableName, Value in pairs(Vars) do
print(VariableName .. " = " .. Value)
end
local Variables = {}
Variables[1] = "1"
Variables[2] = "2"
Variables[3] = "3"
for Key,String in ipairs(Variables) do
print(String)
end
The answer to your question depends on what exactly you’re trying to do, but if you wanted to store information in a table you could iterate through it and achieve relatively the same effect by using the iteration value in the loop to index the table.
local tab = {"1", "2", "3"}
for i = 1, 3 do
print(tab[i])
end
No, but you can store the variables inside a table.
local var = {"1", "2", "3"}
for i = 1, 3 do
print(var[i])
end