A gui button that clears all the duplicated tools in a player's inventory

How do I make a gui textbutton/imagebutton that will destroy all duplicated tools and only leave one of each tools OR only make each of player’s individual tools to only be one?

Any help is appreciated!

make a local script inside button

local Players = game.Players.LocalPlayer
local Tool = Players.Backpack:FindFirstChildWhichIsA("Tool")
local NotDestroyedTool = Players.Backpack:WaitForChild("NotCopiedToolName")

script.Parent.MouseButton1Click:Connect(function()
    Tool:Destroy()
    not NotDestroyedTool -- If This doesn't working, you can reply me for issue
end)
local textbutton = (Your button location)
local plr 

game.Players.PlayerAdded:Connect(Function(player)
plr = player
end)

textbutton.MouseButton1Down:Connect(Function()
local tool = (Your tool location):Clone()
if plr.Character:FindFirstChild(Tool.Name) or plr.Backpack:FindFirstChild(Tool.Name) then

else
tool.Parent = plr.Backpack
end)

or just use a local script and do this

local textbutton = (button location)
local plr = game.Players.LocalPlayer


textbutton.MouseButton1Click:Connect(function()

local tool = (tool location):Clone()

if plr.Backpack:FindFirstChild(tool.Name) or plr.Character:FindFirstChild(tool.Name) then

else

tool.Parent = plr.Backpack
end)

To delete tools that are duplicated, we first need to establish an identity for what a tool is.

The easiest way to identify tools being “unique” is by their name.

The following set up will work for what you’ve specified:
image

-- Local script
script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.DestroyDuplicates:FireServer()
 end)
-- Server script
game.ReplicatedStorage.DestroyDuplicates.OnServerEvent:Connect(function(player)
	local tab = {}
	for _,tool in pairs(player.Backpack:GetChildren()) do
		if not tab[tool.Name] then
			tab[tool.Name] = tool
		else
			tool:Destroy()
		end
	end
end)

If you also want to ensure equipped tools are also deleted, put this in the server script instead:

-- Server script
game.ReplicatedStorage.DestroyDuplicates.OnServerEvent:Connect(function(player)
	local tab = {}
	local tools = player.Backpack:GetChildren()
	for _,obj in pairs(player.Character:GetChildren()) do
		if obj:IsA("Tool") then
			table.insert(tools, obj)
		end
	end
	for _,tool in pairs(tools) do
		if not tab[tool.Name] then
			tab[tool.Name] = tool
		else
			tool:Destroy()
		end
	end
end)

It’s important to note that when players equip a tool, the tool is parented to the player’s character.

Hope this helps :^)

2 Likes