For loops, i cant understand them

I understand what a for loop does but i cant seem to understand it, like what each part of the script does, most videos or explanations do not explain it properly to me, so can someone explain it, like i see this alot:

for i = 1, 1 do
   
end

So can someone explain what it would do?

2 Likes

is a simple loop in Lua that runs once, setting the variable i to 1 and then immediately stopping. It’s similar to saying “once, I set a variable to 1.”

3 Likes

A for loop is a loop that repeats however many times you want it to.

I’d recommend looking here: Intro to For Loops | Documentation - Roblox Creator Hub

For loops use three values to control how many times they run: a control variable, an end value, and an increment value. Starting from the value of the control variable, the for loops will either count up or down each time it runs code inside the loop until it passes the end value. Positive increment values count up, negative increment values count down.

So, for example:

for yourVariable = 1,10 do
    print(yourVariable .. "# in loop")
end

If you run this, you’ll get:

-- 1# in loop
-- 2# in loop
-- 3# in loop
-- 4# in loop
--5# in loop
--up to 10# in loop

Hopefully this explains the functionality.

6 Likes

So basically what it does is run something until it hits the end value?

2 Likes

So if you did

for i = 1,10 do

it would run 10 times?

1 Like

Quite correct. Every time it runs through the loop, it adds +1 to the control variable, and once it reaches the end value, it stops.

1 Like

I’ll try to simplify the i = 1,1

Lets use apples instead:

local apples = {
	"Yellow Apple",
	"Green Apple",
	"Red Apple",
}


for each, apple in apples do
	print(apple)
end

this would print:
Yellow Apple, Green Apple and Red Apple

Now ima use your example but change the second 1 to a 3 and the i to position

for position= 1,3 do
	print(position, "apple")
end

I would get:
1 Apple
2 Apple
3 Apple

Im not sure if this gets you the idea, I’m pretty bad at explaining

1 Like

A for loop takes a either a table, or indices. And it’ll run until they’re done. If you do for i = 1,10 then it will do i=1, i=2, i=3… until it hits i=10.

But you can also make it take steps of 2, instead of steps of 1, by doing i = 1,10,2.

If you feed it a table, it’ll iterate over all the elements.

1 Like

People should understand logic functions & the different datatypes before even trying to code…

1 Like

I wholeheartedly disagree. People should learn by having fun experimenting. Gatekeeping is never the answer.

6 Likes

— number loop

for - begins the loop
(any variable) = (multiplied by) - input 1 is our current number through the loop, and input 2 multiplies it.
(any number) - how many times it loops through.

— table loop

for - begins the loop
(any variable), (any variable) in pairs((any table)) - loops through the table, input 1 gives you how many times it has found something, input 2 gives you the current item of the table.

1 Like

Then they should learn how to search something on the internet instead of creating a topic, that has been answered thousands of times

Plenty of people here that didn’t mind helping out. And a topic doesn’t just resolve 1 question: it can teach plenty of things, to many people. Gatekeeping isn’t the answer

1 Like

You don’t understand, there is no gatekeeping going on here.

A simple google search could have answered this question more in depth and professionally.

If it were more of a complex question I’d understand, like; “should I use for i = 1, #Table do to get the amount of items or for i,v in pairs(Table) do?”

Not only that, when typing the function in the script editor, roblox gives you a straight up example of usage.

2 Likes

And a simple question brings debate, and interaction.

No debate is required for a topic regarding clarification, what you said doesn’t make much sense.

It’s just unnecessary to make a question like this when a simple google search (even searching on the forum) would have already given you an answer.

What part do you not understand?

2 Likes

I understand perfectly fine. I just don’t agree.

So don’t say anything? This devforum post was proof that they didn’t try and experiment. — Which invalids your first reason.

2 Likes

The irony is incredible. You need a good look in the mirror.

1 Like

Nitpicking won’t get you anywhere, and that criticism is technically more in the right than you are.

I’m dropping this argument, don’t be stubborn as that just leaves a bad impression.

2 Likes