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?
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.
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.
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.