Help with Tables

Hello all,

I am creating a cooking system for my restaurant where players will be interacting with models and GUIs.

Below is an excerpt of my menu (Format: [Food Item Name] = {Ingredients})

["Meals"] = {
		["Fish and Chips"] = {"Cooked Fish", "Cooked Chips"},
	}

I will add some context first before I state my question so you can understand.
For the player to make the fish and chips, the player will grab all the ingredients (raw fish etc) and cook them. All ingredients go into the cooking inventory. When a player clicks on a “cooked” item, it will add that item to separate “selected items” gui.

In the “selected items” GUI, it will contain the ingredients listed above
image

When the player clicks on submit, the system will match the selected ingredients to the food item (being Fish and Chips). How would I go about matching the ingredients in the selected GUI to the food name in the table?

Here is what I have tried so far:

local menu = require(game.ReplicatedStorage.Menu)
local selectedfood = ""

for a,b in pairs (menu.Meals) do
     for c,d in pairs (script.Parent.Selected.Items:GetChildren()) do
          if d:IsA("TextButton") then
               if #d:GetChildren() == #b then
                 selectedfood = a
               end
          end
     end
end

I have no idea whether this is even right, but this doesn’t work for me. If anybody could possibly guide me I would be very much appreciated.

Thank you very much.

1 Like

Here is how would you accomplish this:

local menu = require(game.ReplicatedStorage.Menu)

local function GetMeal()
    local selectedfood = {}

    for _, Button in ipairs(script.Parent.Selected.Items:GetChildren()) do
        if not Button:IsA("TextButton") then continue end
        selectedfood[#selectedfood+1] = Button.Text
    end

    for MealName, Ingredients in pairs(menu.Meals) do
        if #Ingredients ~= #selectedfood then
            continue    -- Not possible to be matched.
        end
        for _, Ingredient in ipairs(Ingredients) do
            if not table.find(selectedfood, Ingredient) then
               continue    -- An Ingredient were not found, not matched.
            end
            return MealName, Ingredients
        end
    end
    return nil
end

Firstly, we are getting the selected ingredients from the list gui and adding them to an array to be compared later with all available meals’ ingredients.

Secondly, we are looping through the meals table and every meal’s ingredient to check if it exists in the selected ones if so, the Meal name and its ingredients will be returned, otherwise, the loop will skip it and check the rest.

If there was no meal matched, the function will simply return nil.

1 Like

That’s great, it worked. Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.