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", ... }
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", ... }
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.
3 comments
i didnt ask what a for i, v in pairs loop was. I asked why this worked. Could you maybe further explain?
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'.
I've been using those loops for several months and understand them. I just had this one question.