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

What is difference between "for i,v in pairs() do" and " for i = 1 , #OfChildren do"?

So basically I have cam Across these two methods and said... "Hey could these two methods of looping be alike" but now I want someone to actually tell me if

For i,v in pairs(object:GetChildren()) do and for i = 1, #OfChildren do are alike.

4 comments

1

The left is a generic loop which uses an iterator. The right is a numeric loop which is the fastest way to loop through an array.

0

hmmmm.

0
0

I am not sure what else to say you are comparing two things which have no direct relation.

1 answer

✓ Accepted answer

The for i,v in pairs() do function gets everything in a table or item. v is everything inside that array or item. i is the number of each item. So suppose that you wanted to print all of the items. So you would do print(tostring(v)). Or let's say warn(tostring(v)). Same thing with i.

The for i = 1, 10 do function stores the variable i as a starting value (left) and an ending value (right). 1 is our starting value, while 10 is our ending value. Like in your for loop, you can do for i = 1, #EndingValue do. This can be used to move parts, or even rotating parts.

Hope this is helpful. If you need more help, visit the developer wiki on both types of for loops.

1 comment

0

Also you can use it for fading GUIs out.

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