so if i have a for i,v loop like this
for i,v in pairs(game.Workspace:GetChildren()) do print(v.Name) end
could i do something like this?
for i,g in pairs(game.Workspace:GetChildren()) do print(g.Name) end
Yes, you are free to do that. You can also change the variable for 'i'. Actually, in for
loop, you are assigning variables in the form of i [index/number]
and v[variable]
.
Lemme know if it helps!
Both of the variables in a generic for loop are, well, variables, meaning you are free to change their name as you please. Lua doesn't lock you onto choosing two specific variable names for a loop; that would be pointless and downright stupid.
Now, the breakdown of a generic for loop:
The first variable in a generic for loop is the current iteration. This is equivalent to #table - (#table - valueNumber)
; if the length of the table if 8 and it's the first iteration, 8 - (8 - 1) = 8 - 7, which equals 1. This would then represent the iteration variable's set value. The name of this variable is the standard i
but it can really be anything you want; variable names usually do no matter at all as long as they follow variable naming rules.
The second variable represents the value the loop is current at, and the location of this value is represented by the iteration variable. If the loop is in its first iteration, the second variable would be table[1]
. The standard name for this variable is v
, but again it can be anything you want.