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

i,v in pairs question?

Asked by 4 years ago

I see a lot of

for i,v in next

What does that do? I've never learned that?

3 answers

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

I'll give you a brief explanation of i pairs. Here's an example of an i pairs loop

for i, v in ipairs(game.Workspace:getChildren) do
    --//Gathers everything in workspace
end

the i = index You can change v to whatever you want.

There's 2 types of pairs. ipairs and pairs.

I prefer ipairs, because I've heard it's more effective. In reality, I'm not 100% sure.

Hopefully this helps!

(also, next might be a table, idk)

Ad
Log in to vote
0
Answered by 4 years ago

If you need further help then i,v in pairs is a loop, it brings each item in a certain folder, it can get everything in a table and is useful for indexing items, i would be a number or a key, v would be what the key holds,

for i, v in pairs(workspace:GetChildren()) do
    print(i,v)
end

This should print "1, Camera", "2, Terrain"

With keys you would be able to manipulate dictionaries:

local dict = {["Key"] = 50, ["MoreKeys"] = 100}
for i, v in pairs(dict) do
    print(i,v)
end

This should print "Key, 50"

Reasons to use this:

Maybe you want to find a certain part or value,

for i, v in pairs(workspace:GetChildren()) do
    if v.Name == "Desired part name" then
        print("Found part")
    end
end

Anywho, this is just if you needed further help.

0
Even tho I’m not the one who asked this, TYSM for your help niel iivSnooxy 248 — 4y
Log in to vote
-3
Answered by 4 years ago

Its hard to explain, hopefully this may help https://developer.roblox.com/en-us/articles/Loops

Answer this question