What is the purpose of this line in my script?

Hello, I am a beginner in scripting, and I was following a tutorial on making an obby. When I touch the button, it should make the gate open for 10 seconds (basically a timer):

image

However, I don’t understand what the purpose of “for _, button in pairs(buttons:GetChildren()) do”. Like, can’t I remove this line? Will the game work properly??

Here’s my script:

Any help is appreciated, Thank you!!!

1 Like

the _ refers to the index and the button refers to the value (usually its represented as for i, v) this will add the event to every button

As @Inkthirsty mentioned removing the _ in the for loop would affect it as the button would now represent the index when the _ represents the index as it isnt really necessary and the button is the value which is a instance

Then. what would happen if i remove _ ?

_ is supposed to be the index of the child and button is the child itself.

You shouldn’t remove this for loop, as buttons is a folder containing parts, and you cannot connect Touched on a folder. You have to loop through every part and connect it there.

It wouldnt work it would output an error in the console

oh i get it now, Thank you so much!

i understand it now, thank you so much!!

It was mentioned above but its important to understand that you can replace both “" and “v” with anything, so don’t feel the need to always name the index as ".” Also index can refer to different things based on the context of the loop. If you’re looping through a folder or array, index is essentially how far into the table / folder you’ve gotten. (1 to however long the folder is) And it can also be a name if you’re looping through a dictionary which is a table with named keys.

local Dictionary = {
     ["Key"] = ..., 
     Mine = ..., 
}

local Array = {"Key", 1, 2}