Hello everyone, I recently came across the for i, v in next variant of the for loop and was wondering if its only use was to loop through a list? After experimenting, I found it worked similarly, if not the same as for i, v in pairs? If not, what can it be used for that pairs cannot?
1 | local example = { "apple" , "banana" , "carrot" , "burger" } |
2 |
3 | for i, v in next , example do |
4 | print (v) |
5 | end |
Output from pairs;
apple, banana, carrot, burger
Output from next;
apple, banana, carrot, burger
It just cycles through a table giving its index and its value. For example this is useful for verification or other things to do with verifying.
Edit: I think their the same thing. I use next for everything, cause that's just me.
Example 1:
1 | local AllowedPlayers = { "BradNewTypical" , "Utter_Incompetence" } |
2 |
3 | for i,v in next , AllowedPlayers do |
4 | if game.Players.LocalPlayer.Name = = v then |
5 | print ( "Allowed" ) |
6 | end |
7 | end |
Example 2:
01 | local Ranks = { |
02 | [ "owner" ] = "Utter_Incompetence" , |
03 | [ "admin" ] = "BradNewTypical" |
04 | } |
05 |
06 | for i,v in next , Ranks do |
07 | if game.Players.LocalPlayer.Name = = v then |
08 | if i = = "owner" then |
09 | print ( "Hey it's an owner" ) |
10 | elseif i = = "admin" then |
11 | print ( "Hey it's an admin" ) |
12 | end |
13 | end |
14 | end |
Good for admin commands, I guess and other things.