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

Why do I have to use a full name in pair loops?

Asked by
TMGOR 20
6 years ago

This code works

for _, v in pairs(game.Players:GetPlayers()) do
    print(v.Name)
end

But this code doesn't

local players = game.Players:GetPlayers() 
for _, v in pairs(players) do
    print(v.Name)
end

Why not?

1 answer

Log in to vote
3
Answered by
LeadRDRK 437 Moderation Voter
6 years ago
Edited 6 years ago

You see, when you predefine the children first, the content of it would never change, whether a child has been removed, or added, because you have defined it to a variable, and they never changes unless redefined. But when you use a full name, you are actually calling to get the table of children in execution time, that means that the table is not stationary and could change itself to include the full content of the parent.

You could still use the variable "short name" way, by redefining the variable when it's needed, but it's not recommended.

1
How did I not think of that! Thanks! TMGOR 20 — 6y
Ad

Answer this question