I’m trying to make a tool on hip / back script with multiple parts while this only works with one part that’s named handle. I’ve got it to work when unequipping but when I equip the tool again It breaks. I’m pretty sure the issue with the code is in function onEquipped
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
for i, v in pairs(handle:GetChildren()) do
if v:IsA("Part") then
v.CanCollide = false
v.Name = script.Parent.Name
v.Parent = model
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
end
end
end
end
end
script.Parent.Unequipped:connect(onUnequipped)
function onEquipped()
if handle ~= nil then
handle.Parent:remove()
end
end
script.Parent.Equipped:connect(onEquipped)```