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

How to use "for i, v in pairs"?

Asked by 9 years ago

I'm a below average but new scripter, could someone be kind enough to explain how to use for i, v in pairs? I found no decant tutorials for this. I know you can do something like this

script.Parent.Parent:GetChildren() for i, v in pairs

or something along those lines, and it'll change an entire models brick colour.

1
While we're at it, here's a link to the difference between pairs and ipairs. https://scriptinghelpers.org/questions/4/whats-the-difference-between-pairs-and-ipairs#9 Unclear 1776 — 9y

3 answers

Log in to vote
2
Answered by 9 years ago

Pretty much what pairs does is iterate trough a table Lets say you have table testand you put a bunch of stuff in it

test = {1,2,"a","b","c","Hello World!"}

Alright great, now pairs gives 2 outputs: The first output is the position of the value in the table. Second it will give the value in the table at that position!

It also needs an input, this is going to be our table t obviously

So lets try it out with the table we just created!

test = {1,2,"a","b","c", "Hello World!"}

for i, v in pairs(t) do -- i is the position and v is the value. We also gave it the the table t
    -- here we do stuff with our values!
    print(i, v) -- we print out i and v, which we get as an output in the pairs loop
end  -- just like a for loop, it needs an end so it knows when to stop

If you did this all right the output should be:

1 1
2 2
3 a
4 b
5 c
6 Hello World

Great! Now if you would like to iterate trough a bunch of bricks, you would call :GetChildren() This will return a table of parts and you can go trough them with pairs But ill leave that for you to figure out!

Ad
Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

Well, if you're looking to change an entire model's brick color, then your mind is in the right place! The "for i,v in pairs" is known as a generic loop. It goes through the specified range, and performs whichever function you have listed. I tend to find this easier than a numeric loop for most cases, however either works. For example:

for i,v in pairs(game.Workspace.Model:GetChildren()) do
    if v:IsA("Part") then
        v.BrickColor = BrickColor.new("Really red")
    end
end)
0
Alright, thank you! For :GetChildren()), is what you put in the parenthesis their children too? If so, how does that work? WolfgangVonPrinz 0 — 9y
0
There are not supposed to be any parameters within GetChildren. "v" in this case defines all of their children Shawnyg 4330 — 9y
Log in to vote
0
Answered by 9 years ago

for i,v in pairs is a common loop.

It's entire syntax is- for i,v in pairs (table) do.

The "table" can be replaced with game.Workspace:GetChildren(), for example, making the syntax for the first line for i,v in pairs(game.Workspace:GetChildren()) do

In basic terms, what this does is goes across every value in a table and will reference it as you've put forth in the loop.

I is the number of times it has ran, in a sense. V is the object it's referring to in the table.

In fact, the reason why the two most common variables for it are i and v, is because they stand for index and value. Indexing how many times it has ran, and running through each value. Keep in mind you can use any value for i and v; I instinctively use a and b.

Here's an example:

x = {"Hello", "How are you", 666} -- declaring our table

for a,b in pairs(x) do
    print(b)
end

This would run the loop three times, for each value in the table. Since it's running three times, each time, referencing bas the value in the table it's running with, and you can print both string values and number values, it will print-

>>Hello
>>How are you
>>666

This is the basis of it.

Answer this question