What Is A Loop In A Loop?
If we look at a single loop you will see that the iterator(“I”) starts at 1. It is compared to the goal. If it is less than or equal to the goal then the code inside the loop runs, at the end it returns to the first line of the loop and I is increased by 1 and the process repeats until I is greater than the goal. At this point it will move on to any code below the for loop.
Add the code below to a Script in the ServerScriptService and run it to see the output.
local goal = 10
for i=1, goal do
print("i = “..i)
end
print(“The for loop has finished”)
When we add a second loop inside the original loop the process is exactly the same. The difference is that instead of a print statement inside the loop we have another loop using j. This inside loop will run completely each time the outside loop using I increases.
Add the code to a Script in ServerScriptService and run it to see the output. You will see that j will count to 10 each time that I increases.
for i=1, goal do
for j=1, 10 do
print("i = "..i,"j = "..j)
end
end
print(“all loops have finished”)
Watch the video above to see how I use a special for loop inside a loop to get my character to play all of their animations one after another.
Comments