Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Tizzel40 243 Moderation Voter
5 years ago
Edited 5 years ago

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.

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. User#5423 17 — 5y
0
hmmmm. Tizzel40 243 — 5y
0
There are different types of iterators. https://www.lua.org/pil/7.1.html https://www.lua.org/pil/7.3.html User#5423 17 — 5y
0
I am not sure what else to say you are comparing two things which have no direct relation. User#5423 17 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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.

0
Also you can use it for fading GUIs out. toman655 58 — 5y
Ad

Answer this question