like for example they will have this script
for _, v in ipairs(game.Workspace:GetChildren()) do
end
but what is _ in that script
like for example they will have this script
for _, v in ipairs(game.Workspace:GetChildren()) do
end
but what is _ in that script
The _ is considered a placeholder variable. It's used when you have a variable that you will not need to manipulate whatsoever. Like in the for loop you showed, the _ was in place of a variable that would represent an iterator through the table of workspace:GetChildren(), generally this value is not useful at all. The v variable on the other hand will need to be accessed at some point (since it represents a specific instance), so it is properly named.
I hope my answer helped you, if it answered your question, please remember to mark it as correct.
Note that _ is not representing "an iterator," but rather, it represents the index at which the "v" value occurs.
_ is just te same as i, its just a variable with a different name, but we do _ because i which stands for indxe isnt always used and probarly useless for some people, so you dont have to look at it each time or make your script somehow cleaner.
Always remember that variables can be named however you want, like the i in a for loop, thats a variable
for i = 1, 10, 0.5 do
end
it can also be
for bloxycola = 1, 10, 0.5 do
end
so theres nothing wrong with naming a variable with something else, even if its a loop thing or anything else. or maybe like the "debounce" variable, that can be named anything, but we just name it debounce to make it easier and know whats that variable for, that's why naming stuff the correct way is always helpful.
So back to the beggining,
for _,v in pairs()
is the same as
for i, v in pairs()
nothing wrong with changing a variable's name.
i like to call it a placeholder
it's the start of an index not a placeholder
_ is a placeholder variable. You can't just do "for v in ipairs(a) do" because v will be the index.
Any time you see _ unless they are in variables, they are silencers.
Example:
for _, v in pairs()
in variables:
_Hi _sup hello_there Examples of these are snake case
Silencers are basically useless and serve no purpose except to maybe save like... a byte of code lmao
This discussion is preserved as it was. New answers, comments, and votes are closed.
6 comments
A useless variable.
"an useless variable" nice grammar
Whoops, my bad :)
its just like 'i'
The other answers have covered what _ is. I would like to add that it is good practice to name your variables something that makes sense and displays what the variable contains properly.
Thus, I recommend that instead of using "v," you use a name that makes more sense (such as child).