Can't find the way to destroy a part

When a player equips a knife from the shop, it clones the handle which is weld on the back. The issue is that, when he equips another knife, the handle appears on top of previous handle. Basically, it stacks on the top on previous handles and I don’t want it. Here is my script:

game.Players.PlayerAdded:Connect(function(player)
	
	
	
	player.CharacterAppearanceLoaded:Connect(function()
				
	 local function wieldKnife()
					
			if player.Character then
				    				
				local KnifeValue = player:FindFirstChild("EquippedKnife").Value
				

			
		if KnifeValue ~= "" then		
					
			local knife = game.ServerStorage.KnifeHandle:WaitForChild(KnifeValue)
					
			
					
			local handle = knife:Clone()
			handle.Parent = player.Character
			
			local Weld = Instance.new("ManualWeld")
			Weld.Enabled = true
			Weld.Part0 = handle
			Weld.Part1 = player.Character:WaitForChild("UpperTorso")
			Weld.Parent = player.Character
			Weld.C0 = CFrame.Angles(math.rad(180), math.rad(90), math.rad(-30))
			Weld.C1 = CFrame.new(0,0,0.5)
				
				
		
		elseif KnifeValue == "" then
				
				
				local handleTwo = game.ServerStorage.KnifeHandle.Handle:Clone()
				handleTwo.Parent = player.Character
				
				local Weld = Instance.new("ManualWeld")
				Weld.Enabled = true
				Weld.Part0 = handleTwo
				Weld.Part1 = player.Character:WaitForChild("UpperTorso")
				Weld.Parent = player.Character
				Weld.C0 = CFrame.Angles(math.rad(180), math.rad(90), math.rad(-30))
				Weld.C1 = CFrame.new(0,0,0.5)
				
				
		
			end
			
			
		
			
		end
			
	end
		
		game.ReplicatedStorage.KnifeWield.OnServerEvent:Connect(wieldKnife)

		
	end)
	
		
end)

I think you’re talking about :Destroy()
Example Use:

game.Workspace.Noob:Destroy()

Destroy all previous things on the back when a new thing is equipped, I think that’ll fix your problem.

I’ve done it, problem is that it destroys the current tool, because remotevent is called when the value of equipped tool changes.

Use Tool.Unequipped:Connect() to destroy the tool from the back when unequipped, preparing it to switch to another tool.

Sorry my bad, it’s not a tool that I’m putting on the back. it’s a cloned handle from serverstorage.

I’m pretty sure the error lies here:

local allHandles = game.ServerStorage.KnifeHandle:GetChildren()
local previousKnife = player.Character:WaitForChild(allHandles)

WaitForChild takes a string as an input which is the name of the child you are looking for. You are passing it a list. It should be throwing a timeout error if that’s the case like it did when I tested in studio.

As for a potential solution, I would make it so that any knife handle that ends up in the character, ends up the child of another instance like a model named “knifeHandles” or something purely for organizational purposes. Then instead of trying to delete it like that, I would just clear all the children of that model, then place the new handle on their back and parent it accordingly.

1 Like

You are a legend, that worked perfectly!