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

Running the same for loop multiple times?

Hi, I was wondering if there's another way to make a for loop do something multiple times instead of using 2 loops, like this:

for i = 1,5 do -- Main loop.
for i = 1,3 do -- I want the loop above to do this 3 times w/o using a second loop.
print("Hello")
wait(0.2)
end
print("Goodbye")
wait(0.2)
end

The example I used isn't really what I want to do, but it's the shortest way to explain it. Any help/advice will help!

4 comments

0

What's your actual situation? You might just be able to plop it in a function, then call it whenever you want.

0

I don't have an exact situation yet, however in the past I've used the same method to create Rain. I was wondering if there's a shorter way to write this down instead of using 2 loops.

0

I'm not sure how much shorter you want to get. It takes 2 lines to do something n more times

0

All I was asking is if there was an easier/shorter way to write it, instead of using 2 loops. I don't have a problem with doing the 2 loops, but I wanted to know if there's a way to make it repeat itself w/o doing the second loop.

1 answer

If you have a chunk of code that you want to be able to run on demand, that sounds like you want a function.

For example, maybe you want a function that prints a message 3 times:

-- Define a function called `yell` that
-- takes one parameter (called `text` inside the function)
function yell(text)
	for i = 1, 3 do
		print( text )
		wait(0.2)
	end
end

-- Invoke yell with "Hello" as text:
yell("Hello")
-- Invoke yell with "Goodbye" as text:
yell("Goodbye")

You can of course invoke yell in a loop, too. Here's an example of yelling the first ten squares:

for i = 1, 10 do
	yell(i * i)
end

2 comments

0

That isn't exactly what I meant. I meant is there a way to make a loop do one thing multiple times before it starts over, without using 2 loops.

0

Isn't that what i,v for pairs is for? you could have it so i,v in pairs (does stuff and adds 1 to a value each time) then have a if statement that checks if g > 5 then break

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