Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How does for i,v inpairs(Variable) do work?

Asked by 5 years ago
Edited 5 years ago



Something = script.Parent for i, v in pairs(Variable) do Something.Position = Something.Position + Vector3.new(38, 20, 45) end

I, is the what? v, is what? How do the two relate to loops? How does the variable relate to the loop and something? How do these pieces of concept connect? Inpairs, does what, and how does this work? Can someone help explain this concept for me?

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

**i ** stands for **index **which is the table of things the function needs to iterate ( the repetition of a process) through.

**v ** is the value

So if you do:

game.Workspace:GetChildren()

The table is all the children in the workspace, and the value is the number of them

Here's a detailed answer that goes more in-depth. Hope you learned a thing or two! https://scriptinghelpers.org/questions/8605/what-does-for-_-in-pairs-mean

Ad
Log in to vote
0
Answered by 5 years ago

pairs from what i understand is a fancy shmancy function that returns a function, that is, an iterator function.

the standard way way of using an iterator function with for loops goes as follows:

for <tuple> in <function> do

the function is expected to return some things: - the iterator function (used on every item of the table) - the table that the iterator function operates on - the intitial value that controls if the loop is done

example of ipairs borrowed from the lua manual:

function iter (a, i)
    i = i + 1
    local v = a[i]
    if v then
        return i, v
    end
end
function ipairs (a)
    return iter, a, 0
end

however, iterator funcitions are foreign to me and thats as mush as i can tell you, so you can read more about it here:

https://www.lua.org/pil/7.3.html

Answer this question