-3

I understand, "for i, v" loops for tables, i is the index, and v is the value, but what does this script do? I do not think this has anything to do with tables, but the only type of for table loops I know in ROBLOX script is the first one I mentioned; "for i, v" loops, which loop through tables.

randomVariable = 1
for i = 1, randomVariable do
(random script)
end
Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
  • loops are made to do things *more than once*. if you start at one and end at one, then you're programming in extra steps that aren't needed. your randomVariable would have to be larger than one, or this wouldn't be a loop. – Doyousketch2 Nov 16 '20 at 22:30

2 Answers2

2

This is a numeric loop statement.

for controlValue = startValue, endValue, stepValue do
    -- for body
end

It goes from startValue until it reaches endValue, after running body code, controlValue is increased by stepValue. If controlValue is higher or equals to endValue the loop stops. If stepValue is not provided, it equals to 1.

It's equivalent to this code:

local controlValue = startValue
if not stepValue then stepValue = 1 end -- if no stepValue it equals to 1

while controlValue < endValue do
    -- for body
    controlValue = controlValue + stepValue
end
Spar
  • 1,208
  • 5
  • 13
  • I know that I shouldn't comment thanks, but I don't care; thank you. I have heard of these types of loops but I did not understand the loop in this context, again thanks. –  Nov 16 '20 at 22:23
  • @234fsdafwef They are used to repeat code multiple times. It can be from 1 to 10. or From 100 to -1000. Depends on your needs. – Spar Nov 16 '20 at 22:31
  • @234fsdafwef If you find an answer helpful, be sure to give it an upvote. You can also mark one answer for each question as "accepted". More on this [here](https://stackoverflow.com/help/someone-answers). Also, as you mentioned, "thank you" comments are discouraged. – DarkWiiPlayer Nov 16 '20 at 22:32
-2

There are a few different ways to loop in Lua.

while variable < number do

repeat stuff until variable == number

for key, value in pairs do

for index, value in ipairs do

for i = 1, number do

i = 1 is the initial condition.

It usually begins at one, then loops through items in a table.

So you can think of it as an index in that regard. where the randomVariable you mentioned would be #tab


however, you could set that initial condition to be larger, then count down.

for i = #tab, 1, -1 do

the third, optional argument is called "step size"
and it's the amount that initial condition is changed, after completing each loop. it defaults to 1, so it's not needed, most of the time.

so to step through all the even numbers in a table it would be

for i = 2, #tab, 2 do

Further reading: https://www.tutorialspoint.com/lua/lua_loops.htm

Doyousketch2
  • 796
  • 4
  • 11
  • i, the initial condition, usually begins at one. this isn't hard to follow. it's literally contained within a border, how could it be anything else? – Doyousketch2 Nov 16 '20 at 23:18