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 use of for i,v in pairs do?

Hello i have seen alot of youtube videos on topic and didn't understand much i did understand that it is used for tables but i seen it is used on many other things such as making a round system and many other so my question is what else way can we use it for?

3 comments

0

I,v in pars loop (generic loops) are loops that are able to index a table or get a list of something e.g. Getting all characters

1 answer

✓ Accepted answer

A for-loop is apart of a family known as control-flow; you can denote a form iteration built from a condition to define how the code will iterate through the instructions in it's scope. Alternatively, we can use iterator functions to define specific flows for specific scenarios.

A common iterator-function is the enumeration function. This will allow us to traverse through a list of elements in a form of key-value pairs, meaning we can attain an index (where the element in rotation is in the given table; subsequently, in a Dictionary we can attain a reference) and a value (what that element is)

The Index and Value are usually represented as i and v, but can be named anything we want.

local ArbitraryTable = {4, 6, 1, 4, 5}

for i, v in pairs(ArbitraryTable ) do
	print(i, v)
end 

--[[ Output:
Where in the table:		Value of that element:
	    1						4
	    2						6
	    3						1
	    4						4
	    5						5
]]

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