Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Could I change this part of the code or would it cause errors?

Asked by 10 years ago

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

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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 position index in the list of players, do the following

0
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? M39a9am3R 3210 — 10y
1
ipairs only traverses the indices that are counted in table length. This means for tables that are *lists*, the functionality of `pairs` and `ipairs` is the same. `ipairs` is however slower (for lists), for some reason. BlueTaslem 18071 — 10y
0
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? notes4u99 65 — 10y
1
Editted it into answer BlueTaslem 18071 — 10y
0
I thank you so much for helping me out and taking the time to explain this to me. :) notes4u99 65 — 10y
Ad
Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago

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
0
You would break the script as well, you're making Player index, and as Bluetaslem said, the underscore is a index value you're trying to ignore. M39a9am3R 3210 — 10y

Answer this question