#im new to scripting, can anyone tell me what "i, v in pairs" does?
10 messages · Page 1 of 1 (latest)
It exexutes things for all the variables in a table
I = index
V = Value
Don't really know where to use i but v is useful
Also @toxic wren you do not need pairs or ipairs
do u guys release that he is new
and you have to explain it newie
he doesn't even know what an index is
pairs is a built in function that iterates over a given table. You can use it in a for loop to iterate over the table you want. The "i" and "v" are variables that are assigned to the Key and the Value of the current element.
For ex:
local myTable = {key1 = "value1", key2 = "value2", key3 = "value3"}
for i, v in pairs(myTable) do
print(i, v)
end
This will print "key1, value1", "key2, value2", "key3, value3".
The key of a table is like a variable where you store a value. It can be a number, a string, etc. When you make a table like this:
local myTable = {"value1","value2","value3"}
The language assigns numeric keys to the values.
So if you do:
for i, v in pairs(myTable) do
print(i, v)
end
It would print "1, value1", "2, value2", "3, value3".
Then explain it 💀