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