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

can you change the v in for i,v in pairs() do loops?

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

2 comments

1

Yup, it's just assigning the variable to a different name. I personally always change the i,v, I feel like it is better because i don't really like it, it becomes hard to understand in longer codes. Also instead of doing game.Workspace you can just do workspace

0

Probably more secure to reference via game.Workspace due to the fact that a few years ago there was talk of that getting deprecated. But hey. Live your life you wanna

2 answers

✓ Accepted answer

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.

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