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

What are the differences for these "For" loops?

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

✓ Accepted answer

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 comment

1

Thanks.

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