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

How to use for loops?

I am having trouble learning for I, v loops? What do they mean?

1 comment

0

Like I do not know how you use them.. I don't know what they do...

1 answer

✓ Accepted answer

A for loop is a fixed-repetition loop. As the name 'loop' implies, it is used to repeatedly run a block of code.

There are two types of for loop in Lua: numeric and generic.


Numeric looks like this:

for var = start, stop, step do
	...
end

var is the variable that stores the current iteration. start, stop, and step determine the number of iterations: from start to stop in intervals of step. In many cases, step is omitted and is assumed to just be 1.

An example usage:

for i = 1, 5 do
	print("Iteration: " .. i)
end

--[[ Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
--]]

And another, using step:

local Part = Instance.new("Part", workspace)

for i = 1, 0, -.1 do
	Part.Transparency = i
end

step, as shown here, can be negative. However, if it is negative and the start is lower than the stop, the loop won't run at all. start and stop can also be negative, although that is a bit more uncommon in explicitly-typed numeric fors.

Any of the three modifiers can be variables instead of the numbers themselves, like with anything else in Lua:

local start = math.random(10)
local stop = math.random(11, 20)

for i = start, stop do --Remember, `step` defaults to `1`
	print("Wowza!")
end

Generic fors are a tad more complex. They use a special type of function called an iterator function to 'walk' a Table's contents.

Lua comes with two default iterator functions: pairs and ipairs. They each return two things for each iteration: the Index currently being accessed, and the Value of the Table at that Index.

An example:

for index, value in ipairs(workspace:GetChildren()) do
	print(i .. ": " .. value.Name)
end

There is one caveat: ipairs only works on consecutive integer keys (another word for indices) starting at 1. That is, you have to use the default Table sorting:

{"value1", true, 3, "value4", false, 3.14159} -- This Table is given automatic keys, so it will work with `ipairs`
{name = "Bob", desc = "Owner of the Burger Joint", age = 26} --This Table uses explicit keys, so it will not work with `ipairs`

{name = "Clarence", 16, "buff", [3] = true, [5] = 7} --This Table uses a mixed style. When using `ipairs`, the 'name' key is ignored, keys `1`, `2`, and `3` are found, and key `5` is never reached, since key `4` is not defined.

ipairs is useful, however, in that it always 'walks' the Table in the same order: the order of the keys. pairs, although it will find every single key-value pair without skipping any, has an indeterminate order until it has been used at least once.


Using the break keyword, we can exit a loop (any kind, not just a for loop), before it would normally end:

for i = 1, 10 do
	if i > 6 then
		break
	end

	print("i = " .. i)
end

--[[ Output:
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
--]]

Is there anything more you'd like to know, or anything you need more specific explanation?

4 comments

0

Thank you! You're amazing for helping me. <3

0

pairs and ipairs aren't the only iterator functions c: There's also gmatch and gsub,

0

string.gsub isn't an iterator. And you can make your own!

0

Thanks so much for helping me!

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