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

What are all the generic loop functions?

Asked by 10 years ago

I would really like to know, right now all I can understand is the index function. Would someone mind explaining the rest?

3 answers

Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
10 years ago

Take a look at this:

wiki.roblox.com/index.php/RBX.Lua_Glossary#Loop

Ad
Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

pairs, ipairs and next are the built-in global functions used in generic for loops for indexing through tables. I don't know anything about an "index" function.

local t={'a','b','c','d','e','f',a=1,b=2,c=4,d=8,e=16,f=32}
for _,v in pairs(t)do
    print(_,v)
end

Output:

1   a
2   b
3   c
4   d
5   e
6   f
d   8
e   16
f   32
a   1
b   2
c   4

pairs has no particular order for indices that are non-numerical.

for _,v in ipairs(t)do
    print(_,v)
end

Output:

1   a
2   b
3   c
4   d
5   e
6   f

ipairs does not index non-numerical indices at all.

for _,v in next,t do
    print(_,v)
end

Output:

(same output as pairs)

Calling next(t) will tell you whether or not the table is empty.

The generic for loop can also be used with your own functions:

for v in function(_,n)
    return(n or 0)+1
end do
    print(v)
end

This will count up forever.

0
I don't quite get it... what's the difference? ChipioIndustries 454 — 10y
0
pairs indexes numerical indices in order and then non-numerical indices in no particular order, where the two arguments are the key and the value. ipairs indexes only numerical indices, where the two arguments are the numerical index and its value. A generic for loop using the next function accomplishes the same thing as pairs. Custom generic for functions are uncommon, you will probably never see 1waffle1 2908 — 10y
Log in to vote
-1
Answered by
Dom2d2 35
10 years ago
While true do -- Infinite Loop, crashes if there is no wait()
print("hi")
wait(1)
end
for i = 1,10 do --For loop. This loop will print yo 10 times. 
print("yo")
end

Answer this question