Checking if player has tools from a table in their inventory

Hey there,

I wanted to make a script for my game where if player clicked certain part, the script would compare the player’s inventory with a table of tools in the script. Then, if the player had any of these tools, they would be deleted and replaced with a different one. Thing is, I have no clue how you can compare Backpack’s content to a table.

I thought of making the script somewhat like this, but I would rather use some more elegant solution:

local ToolA = game.ServerStorage.ToolA
local ToolB = game.ServerStorage.ToolB
local ToolC = game.ServerStorage.ToolC

.....ClickDetector.MouseClick:Connect(function(plr)
  if not plr.Backpack:FindFirstChild(ToolA) == nil then
     ToolA:Destroy()
  end
  if not plr.Backpack:FindFirstChild(ToolB) == nil then
     ToolB:Destroy()
  end
ToolC:Clone().Parnet = plr.Backpack
end)

Any help would be appreciated.

local ToolA = game.ServerStorage.ToolA
local ToolB = game.ServerStorage.ToolB
local ToolC = game.ServerStorage.ToolC

ClickDetector.MouseClick:Connect(function(plr)
  if not plr.Backpack:FindFirstChild(ToolA.Name) == nil then
     ToolA:Destroy()
  end
  if not plr.Backpack:FindFirstChild(ToolB.Name) == nil then
     ToolB:Destroy()
  end
ToolC:Clone().Parnet = plr.Backpack
end)
1 Like

Yeah, thank you. You came up with what I already have. That’s not the kind of help I’m asking for, though.

Then why did you make this the solution?

Try this:

local ToolA = game.ServerStorage.ToolA
local ToolB = game.ServerStorage.ToolB
local ToolC = game.ServerStorage.ToolC

local tools = {ToolA, ToolB}

.....ClickDetector.MouseClick:Connect(function(plr)
  local backpack = plr.Backpack
    for _, tool in pairs(backpack:GetChildren())
        if table.find(tools, tool) then
            tool:Destroy()
        end
    end
    end
    
    ToolC:Clone().Parnet = plr.Backpack
end)

This script will first make a table with the tools in it. It will get every tool in the backpack via a loop. We will check if the tool is in the table, if it is we will destroy it. At the end we will add the new tool.

3 Likes