What Wicked said was incorrect, you can loop through a table with a normal for-loop like this:
table = {"one", "two", "three"}
for i = 1, #table, 1 do
print(table[i])
end
Which would print out:
one
two
three
To answer your question:
The difference between a numeric for loop and a pairs for loop is that a pairs for loop only accepts arrays.
When using a numeric for loop to iterate through a table, it would only give you the index, and you'll have to manually fetch the value from the table using the index, but when using a pairs for loop it would directly give you both the index and the value for that index, like so:
local myTable = {"hello", "my", "dude"}
for i = 1, #myTable do
print("Index "..i.." is assigned the value "..myTable[i])
end
for i, v in pairs(myTable) do
print("Index "..i.." is assigned the value "..v)
There's also another thing: a numeric for loop takes less time to run than a pairs for loop.
A simple script would prove this:
myTable = {"one", "two", "three"}
local duration1 = tick()
for i, v in pairs(myTable) do
print(i)
end
duration1 = tick() - duration1
print("The pairs for loop took "..duration1.." seconds to loop")
local duration2 = tick()
for i = 1, #myTable, 1 do
print(myTable[i])
end
duration2 = tick() - duration2
print("The numeric for loop took "..duration2.." seconds to loop")
Which would output the following:
one
two
three
The pairs for loop took 0.0014321804046631 seconds to loop
one
two
three
The numeric for loop took 0.0012853145599365 seconds to loop
(The normal for-loop and the in-pairs loop were both tested in two separate scripts, but I merged them together here)
See the difference?
Another example here, the exact same test was conducted, but both of the loops iterated through a folder containing three Parts inside of Workspace
These were the results:
Part (x3)
The pairs for loop took 0.0021255016326904 seconds to loop
Part (x3)
The numeric for loop took 0.0017952919006348 seconds to loop
Conclusion
A numeric for loop should be used when iterating through a range of numbers and a pairs loop would be more convenient when iterating through a table, as it gives you both the index and the value.
A numeric for loop is also helpful when time is important.
(I encourage you to research more about this.)
2 comments
In simple, in pairs loop's can loop through a table while a normal for loop returns a number only
This may help https://scriptinghelpers.org/questions/4036/what-does-in-pairs-do