✓ Accepted answer
A for loops iterates (goes through) each of the values in a table. Say there was a table containing all the instances (objects) in workspace and you wanted to check if it was a part and if it is then make it a random brick colour.
The script would look something like this (There is an underscore where 'i' is because we don't need the index):
for _, part in pairs (workspace:GetChildren()) do
part.BrickColor = BrickColor.Random()
end
Keep in mind values in a table could be anything! For example it could consist of children of a model, integers (whole numbers), strings (text) and etc. When using a for loop there are two variables you put in. The index and the variable (usually seen as i, v). The index is the index in a table (its quite hard to explain but if you see the wiki you'd understand, the value is basically just the value. I know it sounds really complicated but say there was a table with all the children of a model. There would be no index only the values, and in this case the value would be the part. So because we dont need the index we could replace the i with an underscore and change the v to part.
It would look something like this:
for _, part in pairs (model:GetChildren()) do
print(part.Name)
end
Hope this was easy to understand, feel free to accept this answer.
2 comments
Oh I found out that the index is actually the amount of times it ran, so its quite similar to for i = 1 loop, people also do for i = 1, #children do but that isn't necessary.
That depends on the functionality of the loop. In some cases, that is useful.