How do I use `next` in a for loop

I want to use the next function

I don’t know how please don’t give me a whole script I just want an example

DevHub and YouTube

I am trying to learn this to see if I need it for a cooking system i’m gonna be adding in one of my games. But I do not understand how it works, I have seen a code model use it before but I don’t understand how it worked.

--Module Script
local foods = {
    ["Meats"] = {
        ["Steak"] = {
            ["Image"] = "rbxassetid://0", --just for a test image
            ["CookTime"] = 10,
            ["Color"] = Color3.new(180,0,0),
        },
        ["PorkChop"] = {
            ["Image"] = "rbxassetid://0",
            ["CookTime"] = 10,
            ["Color"] = Color3.new(145,0,0),
        },
    },
}

return foods

And I do not understand how to use for i,v in next, module do

I am trying to make it like this

foods.ChildAdded:Connect(function(child)
    for i,v in next, foodmodule do
        if child.Name == (meatNamesInTheMOdule) then
            wait(meatNameWaitTime)
            child.Color = (meatColorInMOdule)
        end
    end
end)

I would appreciate any help on this subject

1 Like

This message is just used to see the views please don’t report this I just do this too see if my post will appear lol sorry

If you’re trying to search through both the category and the food itself you could do something like

for _, category in next, module do
    for _, name in next, category do
        if child.Name == name  then

        end
    end
end

Just two nested for loops

1 Like

Thank you I can’t go on Roblox Studio right now but I will try this out :smiley:

Also how would I do it for the color? for e.g.

if child.Name == name then
    child.Color = category[name]["Color"]
end

in next is equivalant to in pairs

2 Likes

Then why would people use it??? If it is the same

It’s less typing, some people like the look of it better, and it makes much more sense in certain ways
It used to have a slight performance benefit but with luau that’s lost, it was an inconsequential performance benefit anyways

2 Likes

Would this work at all or no not at all

Oh sorry I made a mistake

for _, category in next, module do
    for name, food in next, category do
        if child.Name == name  then
            food.Color
        end
    end
end

This should work

2 Likes

If you want to go through the meats, you will want to use for i,v in next, foods["Meats] do.

Then you will have access to the index and values of each item. The index(i) will be the name and the table will be v. You can get v["Image"], v["Color"], etc from there.