Tool holstering

I’m trying to make a holstering system for tools but the script only works for 1 time and it breaks the second time I try it.

handle = nil

function onUnequipped()
	if script.Parent.Parent == workspace or script.Parent.Parent.className ~= "Backpack" then
		return
	end

	local char = script.Parent.Parent.Parent.Character
	if char ~= nil then
		local torso = char:findFirstChild("Torso")
		local tool = char:findFirstChild(script.Parent.Name)
		if torso ~= nil and tool == nil then
			local model = Instance.new("Model")
			model.Name = script.Parent.Name
			model.Parent = char
			
			handle = script.Parent:clone()
			for i, v in pairs(handle:GetChildren()) do
				if v:IsA("MeshPart") then
				v.CanCollide = false
				v.Parent = model
					
				handle = v
				if v.Name == "Body" then
				local weld = Instance.new("Weld")
				weld.Name = "BackWeld"
				weld.Part0 = torso
				weld.Part1 = v
				weld.C0 = CFrame.new(0,0,0.6)
				weld.C0 = weld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(90),math.rad(330),0)
				weld.Parent = v
				else
					v:Destroy()
					end
				end
			end
		end
	end
end
script.Parent.Unequipped:Connect(onUnequipped)

function onEquipped()
	if handle ~= nil then
		print(handle)
		handle:Destroy()
	end
end
script.Parent.Equipped:Connect(onEquipped)```
2 Likes

Is there any error? If there isn’t any, can you specify what happens when it “break”?

1 Like

I just rewrote the whole script since this was giving me a headache

1 Like
function onEquipped()
	if handle ~= nil then
		print(handle)
		handle:Destroy()
	end
end

The tool’s handle is being destroyed when the tool’s “.Unequipped” event fires.

1 Like

does it display any error in the second or first try? if not then try adding print()'s where you think there error is

1 Like