Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to initialize multiple local var in for loop?

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?

2 comments

1

Have you tried tables?

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!

1 answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.