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)
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
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.