Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 comment

1

How did I not think of that! Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.