How to make my crafting system more efficient

Hello everyone, I’m making a crafting system, at it works fine. However, I don’t know how to streamline the process of destroying the items required to craft something, after the player clicks the icon.
An example of this is the following:
This appletree requires three apples to make, but I have to check if it’s in the Player’s Character (if the player is holding it). Additionally, I have a craftable that requires four wolf fur and four string, which makes this problem even harder.

Ideally, I am looking to make a function to streamline this process…
I have already tried adding all of the player’s possessions into a table, and then destroying it from the table, but it wouldn’t allow me to do that.

Not to be rude but this is a terrible way of doing this and slow to make new recipes
I suggest to you look into OOP (object oriented programing) and make receipes with them such as

local AppleTree = {
    name = "AppleTree",
    receipe = {"Apple", "Apple", "Apple"}

and then checking if the player has these ingredients really simply by

local PlayerBackpack = Player.Backpack:GetChildren() --getting all the players ingredients
local canCraft = true

for i, ingredient in pairs(Craftable.recipe) do
    if not Player.Backpack:FindFirstChild(ingredient) then canCraft = false break end
    table.remove(PlayerBackpack, i)

if canCraft then
   --craft the item

Note this was just concept code. You do not have to do it in this way

1 Like

That is exactly my approach for checking if the player can craft this recipe. The coding snippet is for destroying the items on the server (after a remote event is fired). The reason why I can’t do the same thing on the localscript as the server script is because I can’t find a way to destroy an object on the list. (can’t do something like table.find(PlrItems, recipe[i]):Destroy()

I am not the best at explaining things, if you still don’t understand, I can try again. tl;dr, I am using OOP (I think)

Well then assuming you have checked if the player has the right number and type of ingredients then why are checking for them to begin with? And why put it in other if statements? Can’t you just loop over the need ingredients and destroy them like that?

if canCraft then
    for i, ingredient in pairs(Craftable.recipe) do

I might be wrong but I don’t think that you can just do Backpack[ingredient], don’t you need to use table.find if you only have the name and not the position of the item in the list?

i have changed it. (text to reach the needed amount to reply)

The problem is, this code will return an error if one of the items needed to craft is being held by the player (It would be in player.character, not player.backpack)

then why dont you just check the player character as well then?