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

what is the difference between for i,v in pairs and for i = 1, # ?

What is the difference between these two loops and when should one be favored over the other?

for i, v in pairs (items) do

end

--vs

for i = 1, #items do

end

1 answer

✓ Accepted answer

Use for i, v when you want to do something with the objects and data inside the table while looping through them.

As for the second one, why does it even exist, it work if u wanna loop something for #items amount, but why tho, for i, v can do the same thing.

1 comment

0

That's why I was asking the question in the first place because why would you use the second one when you have the first one? I guess maybe it uses less data because it doesn't label the index and only gets the value?

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