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

What are the differences for these "For" loops?

Asked by 7 years ago
Edited 7 years ago

So, I've been learning scripting and I came across the for loops but, I see that there is another one. I would like for someone to explain to me what the difference is and how to use it.

for i,v in pairs() do 

^^ I know this type

for a = 1,7,0.5 do 

^^ I know this type.


for _, child in pairs() do

^^ I don't know this type.

1 answer

Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
7 years ago

The only difference between the first loop and the 3rd loop is what variable you are assigning to the index (in the first loop its "i", in the second loop it's "_"

The variable "_" in a script is just used as a placeholder meaning it's not meant to be actually used in the script but rather as the name says, a placeholder

When would you use "_"? When you need to put a variable into the field but don't actually plan on using it

-- Doesn't use the index so put's a placeholder instead
for _, child in pairs(workspace:GetChildren()) do
    print(child)
end
-- Uses the index so we should give it a variable other than "_"
for index, child in pairs(workspace:GetChildren()) do
    child.Name = "Child#"..index
end
1
Thanks. TheLightningRises 56 — 7y
Ad

Answer this question