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?
local example = {"apple", "banana" ,"carrot", "burger"} for i, v in next, example do print(v) 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:
local AllowedPlayers = {"BradNewTypical", "Utter_Incompetence"} for i,v in next, AllowedPlayers do if game.Players.LocalPlayer.Name == v then print("Allowed") end end
Example 2:
local Ranks = { ["owner"] = "Utter_Incompetence", ["admin"] = "BradNewTypical" } for i,v in next, Ranks do if game.Players.LocalPlayer.Name == v then if i == "owner" then print("Hey it's an owner") elseif i == "admin" then print("Hey it's an admin") end end end
Good for admin commands, I guess and other things.