When a function returns multiple values, we use comma to separate them, e.g.,
first, second = unpack({1,2});
print(first);
print(second);
pairs returns two values. The first is the index of its argument, the second is the value at that place.
When we are dealing with lists that don't have any particular ordering, like children lists, the index is not useful. However, since it is the first result, we need to put a name there. The _ is frequently used to indicate a variable that is being ignored.
The _ variable cannot be removed since otherwise you will only be able to get the index, which is what we are ignoring. However, you could replace it with any other name that you would like.
EDIT:
How to read pairs for loops in English
for _,Player in pairs(game.Players:GetPlayers()) do
We could read the above as
For each player Player, do the following
If we added the index, so that it was
for index, Player in pairs(game.Players:GetPlayers()) do
we could read this as
For each player Player at position index in the list of players, do the following
5 comments
Wait blue, what's the difference between pairs and ipairs, I've been using pairs since I've started scripting, but is there a actual difference?
ipairs only traverses the indices that are counted in table length. This means for tables that are lists, the functionality of
pairsandipairsis the same.ipairsis however slower (for lists), for some reason.Thanks! I also have one more question. Sorry if i'm asking too much, but could you tell me exactly what is happening in line 2 in a word form(In my code)? Like in a sentence or paragraph?
Editted it into answer
I thank you so much for helping me out and taking the time to explain this to me. :)