Why is the code not getting a value from the Module Script?

Local Script

local PlayerGui = game.Players.LocalPlayer.PlayerGui.ScreenGUI.Inventory.Slots:GetChildren()
local Ingredient = require(workspace.Ingredients)

local Screen = script.Parent.Parent.Options.Slots
script.Parent.MouseButton1Click:Connect(function()
	for i,v in pairs(PlayerGui) do
		print("Item name:".."/"..v.ItemName.Value.."/")
		print("Ingredient table:",Ingredient)

                print(Ingredient[v.ItemName.Value])
		if v.ItemName.Value == Ingredient[v.ItemName.Value] then
			local i = 0
			repeat i+= 1 until(Screen[i].Occupied.Value == false) 
			Screen[i].Occupid = true
			Screen[i].ItemName.Value = v.ItemName.Value
			Screen[i].Count.Value = v.Count.Value
			Screen[i].ItemImage.Image = v.ItemImage.Image
		end
	end
	
end)

Module Script

local TheValleyFoodIngredients = {
	"Wheat Bundle",
	"Flour Mound",
}
return TheValleyFoodIngredients

This line: print(Ingredient[v.ItemName.Value]) prints nil, even when v.ItemName.Value is “Wheat Bundle”. I also checked the spelling and whitespace. That’s all good, but not this.

I’m assuming you want the number that value represents in the table.

table.find(Ingredient, v.ItemName.Value)

This returns the number that value occupies within the table.

That’s not what I’m doing. The code is cycling through the player’s inventory slots and checking to see if any of those items fall under the category of “Ingredients”, and display that item on the screen to be selected to be used in with the molcajete I’m making. If you’re asking about i, all the slots are numbered, so it’s a way for me to cycle through the slots and check each one to see whether it meets the requirement.

Because the items in the table are a Value, not an Index.

when no index is given to the Table value, it gives it a number index, which in your case would look like this:

local TheValleyFoodIngredients = {
	[1] = "Wheat Bundle",
	[2] = "Flour Mound",
}

Are you trying to do this?

["Wheat Bundle"] = value

That might work. Would I have to change any of the formatting in the other script to get this to work correctly?

Change the module script to this:

local TheValleyFoodIngredients = {
	["Wheat Bundle"] = "Wheat Bundle",
	["Flour Mound"] = "Flour Mound",
}
return TheValleyFoodIngredients

If you want to remove a recipe, you can just set the value to false. No need to change the local script

Btw please set this as the solution if it worked, thanks!

1 Like

If you are trying to get “Wheat Bundle” or “Flour Mound” from the table, no, but its the way you are setting the values that appears to be the issue.

It didn’t work, because now Ingredient[v.ItemName.Value] is returning as true, not “Wheat Bundle”

Ah ok, here is a quick fix:

local TheValleyFoodIngredients = {
	["Wheat Bundle"] = "Wheat Bundle",
	["Flour Mound"] = "Flour Mound",
}
return TheValleyFoodIngredients

Does it work now?

1 Like

Yes it works! Thank you for helping out.

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