I’ve been trying to make a crafting system for my game. My recipe is inside a table:
I already made the script take all items from a player and turn it into a table:
output:
Now i need to some how check if the player has enough items
I’ve been trying to make a crafting system for my game. My recipe is inside a table:
I already made the script take all items from a player and turn it into a table:
output:
Now i need to some how check if the player has enough items
Slightly unrelated but that else expression could just become Items[v.Name] = 1
.
Regarding your question, you’d just need to check if the player has the required material type & quantity.
Here’s an example script with comments attached detailing each step.
local function CraftItem(ItemName) --CraftItem function, requires the item's name to be crafted.
local Item = Craftings[ItemName] --Verify that the item's name corresponds to an existing recipe.
if Item then
for MaterialType, MaterialQuantity in pairs(Item) do --Iterate over the recipe materials & quantities.
local PlayerMaterialQuantity = Items[MaterialType] --Get the player's quantity of the material type.
if PlayerMaterialQuantity then
if MaterialQuantity > PlayerMaterialQuantity then --Check if the player has less than the required material quantity.
return false --Player does not have the required material quantity.
end
else
return false --Player does not have the required material type.
end
end
--Code here is only executed if the for loop executes without being prematurely broken out of.
return true --Player has the necessary material types & quantities to craft the specified item.
else
return false --Invalid item specified.
end
end
Hey, I got an error “Argument 1 missing or nil”, what do I do?