This is known as an iteration. It’s a fancy way of saying a loop. We give 3 things to a loop of this type:
- The start number (start)
- The end position (stop)
- How many times it should go up by each time (step)
So, if we do this:
for i = 1, 10, 1 do
print(i)
end
we will get this:
1
2
3
4
5
6
7
8
9
10
This is because each time we loop round, the current loop count is stored in the variable i
. So, we want to get the length of our message. We can do this by using the hashtag.
print(#"hi there") --> outputs 8
So, we get our length and put it in the loop!
for i = 1, #"hi there", 1 do
print(i)
end
The only difference before is that we used a variable to store our message beforehand. A variable is a pointer to a location in the memory that can change throughout the runtime of a program. Basically, it holds a value and you can change what value it holds whenever you want to.
Incremented means to increase.
We then use our string.sub
function to get the snippet of the string between 1 and i, and since i is incremented by 1 each loop, we get +1 character from the string each loop!
local hello = "hello"
for i = 1, #hello, 1 do
print(string.sub(hello, 1, i))
end
This would output:
h
he
hel
hell
hello
So, in short, we loop over the string, get a snippet from it, and put that on the text.
There are other types of iteration (loops), but I won’t talk about those here.
Sorry, this is a bit long so it might get a bit confusing, hope it helps 