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