Remove one tool with the same name

  1. What do you want to achieve? Hello, i want to remove only one tool from the player inventory with the same name. Here is the script :
if player.Backpack:FindFirstChild("ToolToRemove") then
				player.Backpack:FindFirstChild("ToolToRemove"):Destroy()
			end
  1. What is the issue? If the player has more than 1 tool with the same name the script will delete multiple of them

  2. What solutions have you tried so far? I tried to fix it myself and looked on forums

1 Like

The code you sent shouldn’t be removing multiple items unless you are running it multiple times. Just run it once if that’s possible.

Im running it once but it remove multiple of them

Do you have multiple instances of the same script executing this code?

Yes, there is only one script that does this

Where is that script located and how/when does it run?

The script is located in a model and it runs with a clickdetector

local Tools = {}
for i,v in pairs(player.Backpack:GetChildren()) do 
	if (v:IsA("Tool")) and (v.Name == "ToolToRemote") then 
		table.insert(Tools, v)
	end
end
Tools[1]:Destroy()

Put in a print() function into the code to see if it somehow runs multiple times and show us the output. I really don’t see how it could remove multiple tools by running just once.

1 Like

If you want a specific instance to be deleted then you need to store that instance itself as there is no other way to separate two identical instances without storing one. For example:

local MyTool = ReplicatedStorage.Gun:Clone()

wait(10)
-- ok gun needs to go away now
MyTool:Destroy()

An example of how you would achieve this in a proper way though:

local PlrWeaponsToRemove = {}
function OnChar(Player,Character)
	local MyGun = game.ReplicatedStorage.Gun:Clone()
	MyGun.Parent = Player.Backpack
	PlrWeaponsToRemove[Player.Name] = {
		MyGun,
	}
	
	wait(5)
	for i,v in ipairs(PlrWeaponsToRemove[Player.Name]) do 
		v:Destroy()
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		OnChar(Player,Character)
	end)
end)

Alternatively you can just use Attributes and give each tool a unique Identity. Read about attributes here:Instance Attributes | Roblox Creator Documentation

Wait i think i found my problem, im gonna explain it to you

Okay, so in my script i actually have 2 tools checker

if player.Backpack:FindFirstChild("ToolToRemove") then
				player.Backpack:FindFirstChild("ToolToRemove"):Destroy()
			end
			if player.Character:FindFirstChild("ToolToRemove") then
				player.Character:FindFirstChild("ToolToRemove"):Destroy()
			end

And when i hold one while triggering the tool remove it removes multiple of them

Well if your removing code is running twice then two tools will be removed…
Can you just remove the second removing code?

But if the player is holding the tool it wont remove it if i remove the second removing code

Oh okay I understand now.
You could first check if the player is holding it, if yes then remove it, otherwise (elseif statement) check if it’s in the inventory. This way it won’t remove both the one in their hands and in the inventory.

if player.Character:FindFirstChild("ToolToRemove") then
	player.Character:FindFirstChild("ToolToRemove"):Destroy()
elseif
	player.Backpack:FindFirstChild("ToolToRemove"):Destroy()
end

It worked, i didn’t think of that, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.