Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to initialize multiple local var in for loop?

Asked by 3 years ago

Basically, I have the same 30 local variables with the incremental numbers in the end. What I want is this end result

for num = 1, 20 do
    local Question..num = script.Parent.Question..num..Frame
end

Instead of the messy lines of code like this:

local Question1 = script.Parent.Question1Frame
local Question2 = script.Parent.Question2Frame
local Question3 = script.Parent.Question3Frame
-- So on 

How do I go about using a for loop for this situation?

1
Have you tried tables? Qariter 110 — 3y
0
I haven't thought of using tables. I'm not entirely sure how I could use them in this case but I'll experiment with it and see what I can come up with! NanotechSci 40 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I figured out the solution! By using tables as suggested by Qariter and learning how to convert String to Instance, from this link: https://devforum.roblox.com/t/convert-string-to-instance/385716 I've come up with this block of code:

local Question = {}
for index = 1, 20 do
    local dir = "Question"..index.."Frame"
    local segments = dir:split(".")
    local current = script.Parent --location to search
    for i,v in pairs(segments) do
        current=current[v]
    end
    table.insert(Question, current)
end

I tested it out and it worked successfully!

Ad

Answer this question