Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What can 'for i, v in next, list do' be used for?

Asked by 4 years ago
Edited 4 years ago

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

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
Alright, thanks for clarifying! Utter_Incompetence 856 — 4y
Ad

Answer this question