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

What is the different between for _,v and for i,v ?

I'm very confused on what the difference is between for _,v and for i,v. Here is an example of those 2 for statements being used;

for _,v

local Names = {"Player1","Player2","Guest1000"}

for _,names in pairs(Names) do
	--blah blah blah codes idk
end

for i,v

local Names = {"Player1","Player2","Guest1000"}

for i,names in pairs(Names) do
	--blah blah code idk xd
end

Any help?

2 answers

✓ Accepted answer

What's the difference between for _,v and for i,v? Actually, there's no major difference. When you use a _ (underscore) you're actually just removing the index (or i as most people define it). The variable(s) within a for loop can be defined as anything.

Examples For example, I could do:

local myTestTable = {"Hello World!"};
for index, value in pairs(myTestTable) do
	print(index, value);
end

That's equivalent to doing:

local myTestTable = {"Hello World!"};
for i,v in pairs(myTestTable) do
	print(index, value);
end

Conclusion Basically name the variables in your loops the way you'd like to name them. Back to ,v if you know you're not going to use the index (i) then some developers will just put an underscore (). When they put that underscore they know they won't be using the index and it's used to prevent the defining of another variable.

I hope I answered any questions or concerns you may have had :)

1 comment

0

You are not removing the index. It is just renaming it, so you need to do _ to access it. _ is the placeholder variable, so people use that when they don't need that part.

Some people like using for _,v instead of for i,v because they like eliminating the index value as a reminder that they're not going to use it in their code. It's just a style to.

I might be wrong, I don't know either o-o

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