Attempting to create a crafting system by comparing recipe to found parts

I am attempting to create a crafting system that finds the parts that the player just drops into a cauldron by using a region3, then compares it to the ingredients required in any recipe to find a match and craft it when a click detector is clicked.

The script works by trying to find the needed ingredient from the recipe in the cauldron, but even when a Flower is in the cauldron and the script is attempting to find one, it spits out “nil”.

I have been following this post but I can’t seem to figure out why it keeps spitting out “nil” so I am looking for a point in the right direction.

The bold is the issue

local AlchTable = game.Workspace:FindFirstChild("AlchemyTable")
local values = game.ReplicatedStorage.Ingredients
local recipes = require(game.ServerScriptService.modules.recipes)

local Point1 = AlchTable.Points:FindFirstChild("Point1").Position
local Point2 = AlchTable.Points:FindFirstChild("Point2").Position


local Region = Region3.new(
	Vector3.new(
		math.min(Point1.X, Point2.X),
		math.min(Point1.Y, Point2.Y),
		math.min(Point1.Z, Point2.Z)
	),
	Vector3.new(
		math.max(Point1.X, Point2.X),
		math.max(Point1.Y, Point2.Y),
		math.max(Point1.Z, Point2.Z)
	)
)

--[[TEST
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
part.Size = Region.Size
part.CanCollide = false
part.CFrame = Region.CFrame
print(Region.Size) ]]


AlchTable.Stir.ClickDetector.MouseClick:Connect(function()
	local ingredientTable = workspace:FindPartsInRegion3(Region)
	print(ingredientTable)
	
	**for RecipeName, Recipe in pairs(recipes) do**
**		print(RecipeName, Recipe)**
**		for ingredient, quantity in pairs(Recipe) do**
**			print(ingredient)**
**			print(ingredientTable[ingredient])**
**			if ingredientTable[ingredient] <= quantity then**
**				print("me no craft")**
**				break**
			end
		end
	end
end)


so the region3 parts are returning nil?
In your test function, have you tested putting random parts in the cauldron?

1 Like

The region3 is returning it as being there, its when the script is trying to find “Flower” within the region3 table it returns “nil” even with flower being apart of the table

1 Like

The region3 indexes it’s parts by number, not by name. The ingredientTable has the part named “flower”. But nothing is indexed under flower in it since GetPartsInRegion3 returns a table of parts similar to GetChildren.

It would get a lot more tedious, but you can go through the process of using a sort function to get the part names and translating them into something more compatible with your system.

local ingredientTable = workspace:FindPartsInRegion3(Region)

local translation = {}

for _,ingredient in pairs(ingredientTable) do
if translation[ingredient.Name] then
translation[ingredient.Name] +=1
else
translation[ingredient.Name] = 1
end

end

--beyond this point the translation table should be more compatible with
your current system

After this, depending on how complex your crafting system is going to be you can make a failsafe that
if your translation matches a recipe entirely, you’ll get your item rather than matching your recipe to your translation. Your translation may be aiming for a complex recipe, but if your translation matches your complex recipe as well as other simple ones, you could run into some problems. Good luck.

1 Like

ohh okay that makes a lot of sense thank you very much I will try this.

1 Like

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