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

Could someone explain how "nesting" works to me with pairs loops?

Asked by 4 years ago
Edited 4 years ago
local admins = {"GoodAdmins", "b"}


for i,v  in pairs(admins) do
    if game.Players:FindFirstChild(v) then
    game.Players[v].Chatted:Connect(function(msg)
        print(msg)
        end)
    end
end

This script works perfectly fine, but why? Isn't v the last value the loop went through? Shouldn't v be b? Why does this script work?

3 answers

Log in to vote
2
Answered by
gskw 1046 Moderation Voter
4 years ago

In your code, the variables i and v receive different variables on each iteration. In this case, there are two iterations, and the value v receives is "GoodAdmins" on the first iteration, and "b" on the second iteration. To put things simply, your code is functionally equivalent to the following:

local i = nil
local v = nil

i = 1
v = "GoodAdmins"
if game.Players:FindFirstChild(v) then
    game.Players[v].Chatted:connect(function(msg)
        print(msg)
    end
end

i = 2
v = "b"
if game.Players:FindFirstChild(v) then
    game.Players[v].Chatted:connect(function(msg)
        print(msg)
    end
end

In other words, v isn't the last value the loop went through. The code inside the loop is executed twice, and v has a different value each time. After the loop has been executed, v goes out of scope, that is, it is effectively set to nil. More precisely, v is a local variable inside the for loop, but outside the loop it isn't defined, and is considered a global variable (unless you've explicitly created a variable with that name outside the loop). The same goes for i as well.

Ad
Log in to vote
0
Answered by 4 years ago

Nesting with loops, is where you put a loop inside a loop.

Example...

for a,b in pairs(admins) do -- a represents the index in the table 'admins', b is the value
    for c,d in pairs(b) do  -- c represents the index in the table 'b', d is the value
        print(b,d)
    end
end

The 'in pairs' part means it's giving you a pair, the index and the value at that index. The loop itself will go through every index inside of the table 'admin'. So to answer your question, technically yes, when you're at the last index of the table 'admin', v will be equal to "b", and 'i' will be equal to 2 since Lua is 1 based.

0
i didnt ask what a for i, v in pairs loop was. I asked why this worked. Could you maybe further explain? goodadmins 111 — 4y
0
I think you need to understand the basics such as what a loop is and how it functions before you start using different variations of them such as 'in pairs' or 'in ipairs'. climethestair 1663 — 4y
0
I've been using those loops for several months and understand them. I just had this one question. goodadmins 111 — 4y
Log in to vote
0
Answered by 4 years ago

If your question is in reference to the code you provided, you're not really "nesting" anything. As explained in climethestair's answer, nesting is when you have one or more loops within a loop, which I don't see here. I gather that you don't quite understand how in pairs (Or "for-each" in other languages) loops work (Or what their purpose is).

First off, let's think about a classic for loop:

local nums = {"one", "two", "three"}

for i = 1, 3 do
    print(nums[i])
end

The output will be exactly what you can gather from looking at it 1-3 printed in word form! Not too bad, eh? What if, however, we wanted to print numbers from an array of unknown size? Not a big deal: Just change the final index to be the length of the array we want to print.

local nums = {"one", "two", "three", "four", ... } --(who knows how many more?)

for i = 1, #nums do
    print(nums[i])
end

That's great an all, but (if I'm being honest) it's a pain to code like this. Everytime you want to access the current value of nums, you have to write out the entire "nums[i]" thing, which gets old real quick, not to mention the clutter it adds to your code. There has to be a better way...

Enter the "in pairs" loop:

local nums = {"one", "two", "three", "four", ... } --(who knows how many more?)

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

This code does exactly the same thing as the code above, but in a much neater and easier to understand way.

The main change here as you can see is the for i,v in pairs(nums) do part. This causes every new loop of the for loop to read the next value of your array, and update "i" and "v" ("i" is the index of the current value "v") to those new values. That means that "i,v" will actually change in value as your loop runs. This basically lets you loop through every value in an array in a simple, clean fashion and run some code on each one.

So, in your code

local admins = {"GoodAdmins", "b"}


for i,v  in pairs(admins) do
    if game.Players:FindFirstChild(v) then
    game.Players[v].Chatted:Connect(function(msg)
        print(msg)
        end)
    end
end

What will happen is that on the first loop of the for loop is that, "i" will be 1, v will be equal to "GoodAdmins" and on the second, "i" will be 2, v will be equal to "b"

If you try adding more admin names to your array, you'll see that they also work, because you're looping through all of them.

0
So you're saying every time v is called, the loop loops again? goodadmins 111 — 4y

Answer this question