Could I change the part of code in line 2 to _ , _ or is there a reason why _ , Player is there? If there is a reason why that is there could you tell me what the reason is? Thanks.
while wait(5) do for _,Player in pairs(game.Players:GetPlayers()) do if Player:FindFirstChild("leaderstats") then Player.leaderstats.Money.Value = Player.leaderstats.Money.Value +1 end end end
When a function returns multiple values, we use comma to separate them, e.g.,
first, second = unpack({1,2}); print(first); -- 1 print(second); -- 2
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 positionindex
in the list of players, do the following
Some of the wiki has the script as;
for i, v in pairs() do
Because i is the index value or it's position in the supposed table (:GetChildren() and :GetPlayers() return read only tables, v is the value, but in your script Player is Value. Yes, your script will break if you change _, Player to _, _.
So for instance I wanted to kick the third player
for i, v in pairs(game.Players:GetChildren()) do if i == 3 then --Third player to join. v:Kick() --v is the player, s/he gets kicked. end end