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

When Is It Better To Use A for in pairs loop, vs a regular for loop?

Hello guys i am trying my hardest to understand the difference between regular for loops, and for i,v in pairs loop.

Which is better for what purpose and could i please have an example for both in cases.

I know the basics of

for i,v in pairs do
end

and i know the basics of a regular for loops

but i dont know what real difference there is to achieve something different that they both cant do? I asked someone before and they said

"pairs returns the object and a normal loop only returns the iteration"

what do they mean by "object"?

Could anyone give me a little help understanding. Thank you so much

2 comments

0

In simple, in pairs loop's can loop through a table while a normal for loop returns a number only

3 answers

✓ Accepted answer

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"}

--Numeric for loop
for i = 1, #myTable do
    print("Index "..i.." is assigned the value "..myTable[i])
end

--Pairs for loop
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()

--Pairs for loop
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()

--Numeric for loop
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.)

3 comments

0

Forgot to add, using this method, the in pairs loop gives you the key and value while a regular for-loop only gives you the key.

0

Thank you so much this help me understand a lot <3

0

Just a side-note though, there might be situations where one works better than the other (i.e iterating through a range of numbers is better with a normal for-loop), but I would say both work for a lot of things.

First of all, hope this helps and you will get better at Lua.

The difference between a for in pairs loop and a normal for loop is that a pairs loop can additionally loop through a table or an array table while a normal for loop cannot. Please mark as answered for this answer if this helped! That will be amazing!

1 comment

0

You can use normal for loops to run though tables.

I use normal for loops when I need to repeat something a # of times. I use pairs/ipairs loops when I need to scan an array or table for whatever reason.

Check out these examples:

Normal:

for i = 1, 10 do -- Make 10 parts
	local Part = Instance.new("Part")
	Part.Parent = workspace
end

Pairs:

for _, Object in pairs(workspace:GetChildren()) do -- Destroy all parts in the workspace
	if Object:IsA("Part") then
		Object:Destory()
	end
end

Hope this helps.

Patch

1 comment

0

Thank you man that helped a lot

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