A numeric loop is used to do something a specific amount of times.
Here’s like a “disection” of it.
for
is just a type of loop.
i
is the name of the variable that can be referenced inside of the loop
= num1, num2, num3
num1 is the initial number, num2 is the “goal”, num3 is the step used to reach this goal. num3 is also optional and is 1 by default
for i = 0, 10, 0.1 do
print(i) -- will print 0, 0.1, 0.2, 0.3 etc... all the way to 10 because we used 0.1 as the step
end
Your example would probably error because text is already predefined as a string, you would have to use len on the string.
local text = 'Hello here is my text!'
for i = 1, text:len() do
print(text:sub(1,i) -- would print H, He, Hel, Hell, Hello etc...
wait()
end
Also as @OptimisticSide said, there are multiple types of for loops. There are pairs, ipairs, and next iterators.
They all do somewhat close to the same thing but have some key differences.
pairs is used to iterate through a table or dictionary (which may have mixed keys, number keys, or keys with gaps in them). The i and v are used to reference the index and the value. The i would be ‘key1’, the v would be 1, then in the next iteration, the i would be key2, value would be {} etc…
The downside to using pairs is that it will not always work in order so the first iteration might be i = key3, value is Color3.new(). Keep in mind though, the index will always correspond with the value so if you aren’t too worried about it being in order, it’s not an issue.
So for example,
local myDictionary = {
['key1'] = 1;
['key2'] = {};
['key3'] = Color3.new()
}
for i,v in pairs(myDictionary) do
print(i,v) -- would print key1 1, key2 {}, key3 0,0,0 etc...
end
ipairs works similarly to pairs except it will only work for tables that have consecutive number indexes.
local myTable = {
[1] = 'string';
[2] = {};
[3] = Color3.new()
}
for i,v in ipairs(myTable) do
print(i,v) -- prints 1 string, 2 {} etc...
end
This will go on as long as there isn’t a nil value. But what happens if it runs into a nil value is that it will break (exit, not error) the loop.
local myTable = {
[1] = '1';
[2] = '2';
['this is a string key'] = '3'
}
for i,v in ipairs(myTable) do
print(i,v) -- will print 1 1, 2 2 but will never print "this is a string key 3" because it is a non-number value
end
Same thing happens if we have a missing value.
local myTable = {
[1] = 1;
[2] = 2;
[4] = 4; -- note we will never see this because of the missing 3 index
}
for i,v in ipairs(myTable) do
print(i,v) -- will print 1 1, 2 2 but never 4 4.
end
The final iterator is the next iterator, probably the most unused one since it’s pretty much the same as pairs. In fact, on the backend, pairs literally uses next to work.
local myTable = {
['key'] = 'v';
['key2'] = 'v2';
}
for i,v in next, myTable do
print(i,v) -- prints key v, key2 v2
end
Here’s the “raw” pairs.
function pairs(t)
return next, t, nil
end
Then there are repeat
and while
loops.
While loops basically do something while the condition is true.
local var = 1
coroutine.wrap(function()
while var ~= 2 do
print('var is not 2')
wait()
end
end)()
wait(3)
var = 2
This will print ‘var is not 2’ for 3 seconds since the var
variable gets assigned 2. Something you might also see is while true do
or while wait() do
. The reason people do this is to do something infinitely. The reason while wait do
works is because wait() returns 2 values which aren’t nil. And I assume you have a basic idea of how if statements work, if we do if wait() then
, it will be considered true.
The final loop is repeat until
loops. They are basically the same as while
loops but kind of the opposite. It will repeat something until the condition is true
local var = 1
coroutine.wrap(function()
repeat
print('var is not 2')
wait()
until var == 2
end)()
wait(3)
var = 2
This will print ‘var is not 2’ for 3 seconds as var
is assigned 2. If you read it out loud, it will make sense. Repeat ___ until var is equal to 2
Anyway, that was a basic rundown of loops, let me know if you have any questions!
Edit:
I left out a few things:
table.foreach passes each index and value to a function:
local function printTable(index, value)
print(index, value)
end
table.foreach({['String'] = 'StringValue'; ['String2'] = 'StringValue2'}, printTable)
--> 21:17:07.208 String2 StringValue2 - Edit
-- 21:17:07.208 String StringValue - Edit
table.foreachi works similarly to table.foreach in the sense of iterating through the table but it iterates through the length of the table and doesn’t stop when the value is nil unlike ipairs.
local function printTable(index, value)
print(index,value)
end
table.foreachi({[1] = 'value1'; [2] = 'value2', [4] = 'value4'; ['str'] = 'valuestring'}, printTable)
--> 21:19:34.628 1 value1 - Edit
-- 21:19:34.628 2 value2 - Edit
-- 21:19:34.628 3 nil - Edit
-- 21:19:34.628 4 value4 - Edit