for i, player in pairs(Players:GetPlayers()) do
end
I see this a lot in scripts that interact with the players list but I don't know what it means, specifically "I" and "player in pairs"
for i, player in pairs(Players:GetPlayers()) do
end
I see this a lot in scripts that interact with the players list but I don't know what it means, specifically "I" and "player in pairs"
A for loop is apart of a family known as control-flow; you can denote a form iteration built from a condition to define how the code will iterate through the instructions n it's scope. Alternatively, we can use iterator functions to define specific flows for specific scenarios.
A common iterator function is the enumeration function. This will allow us to traverse through a list of elements in a form of key-value pairs, meaning we can attain an index—where the element in rotation is in the given table—and a value—what that element is.
The Index and Value are usually represented as i and v, but can be named anything we want.
local arbitraryTable = {4,6,1,4,5}
for i,v in pairs(arbitraryTable) do
print(i, v)
end
--[[ Output:
Where in the table: Value of that element:
1 4
2 6
3 1
4 4
5 5
]]
Regular For loops
Let's talk about the most common use of a for loop. As aforementioned above, these loops can define how we loop (traverse or iterate) over a scope of instructions. A normal for initialization follows a format that can be visualized like so: start stop step (optional). By evaluating these numbers, they will determine how the loop shall act. Let's take a look at a few examples:
for i = 1,10 do
print(i)
end
This tells the for loop to iterate over this print() call from a range of 1 to 10 (up to 10 times). i represents the current iteration we're on in respect to the range given:
--[[ Output:
1
2
3
4
5
6
7
8
9
10
]]
Let's take a look at an example with step, this defines how we step through the numbers from 1-10, default, it's an increment of 1, but let's say we want to step to every second number:
for i = 0,10,2 do
print(i)
end
--[[ Output:
0
2
4
6
8
10
]]
for i = 10,0,-1 do --// From 10, to 0, step back 1
print(i)
end
--[[ Output:
10
9
8
7
6
5
4
3
2
1
0
]]
TL;DR: It goes through tables and allows us to perform actions on each individual attribute. In the example you have, it runs through each player of the game.
Wow simple but very well put. But I think you should've included a use case for like for i = 1, 1500, 5 do. Something like that.
Alright, I'll include a normal for statement explanation.
aren't if statements associated with "control flow"?
Yes, they are.
so in a for loop, you are usally getting a dictionary, the dictionary has numbers for all of its data like this:
local myDictionary = {"hello","world"}
if I wanted to get hello for instance, you could do this
local myDictionary = {"hello","world"}
print(myDictionary[1])
that would print the first thing in the dictionary, so hello
when using a for loop, you could get things by their number, or there actual object(I and player)
the "pairs" part means to get the dictionary in two parts, I and player.
so I and player are variables to use in the for loop
if you don't need I, you could do this by doing "_" so
for _,player in pairs(Players:GetPlayers()) do
end
but in this case you can only use player, not I
This does explain it but not very well. Firstly, you say you can get things by their "number" which is an index and second you say something about the actual object being i which is false. And even when the underscore is present it is still usable. print(_) would just print the index. And that last statement was incorrect so downvote for that.
Also you don't explain how to use it in other ways. So also downvoted for that.
@suspectshot108 when you are explaining a script you dont have to give out every single detail of something
This is true but his code still had incorrect information in it.
@suspectshot108 jeez, I just tried my best to explain and I get shot down for it, thanks I gess
I'm just saying know your facts before posting an answer. If you're not sure refer to the wiki. Or post a comment.
Sorry
Essentially what a for loop does is it can either take index or an index and a variant. An example of both of these are
Index only
for i = 1, 10 do
print(i)
end
This would print every number from 1 to 10. You can also use this as a loop to do something 10 times. There is another way to put this though that uses 3 numbers.
for i = 10, 1, -1 do
print(i)
end
So this will count down from 10 to 1 because we sent our increment to -1. That is the third value.
Now another way to use it is to use it on a table.
Here are some examples
for i,v in pairs(game.Players:GetPlayers()) do
print("Index is "..i.." variant's name is "..v.Name)
end
As you can see, this returns an index and an object. The object being a player because Players:GetPlayers() returns a table of every player on the server.
For more info you can go here
Thank you!
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
https://developer.roblox.com/en-us/articles/Loops try this from roblox developer webiste