Hello, i’ve trying to create an OOP module for my guns, client-sided. When the gun is destroyed, i connect the .Destroying event to unequip and destroy the whole gun, but it never fires, even tried with .AncestryChanged, it does fire, however, when the parent is nil, it does not.
Here is the function that binds everything when gun class is created:
function GunClient:_bindToolEvents()
--TOOL
self.Tool.Equipped:Connect(function()
self.IsEquipped = true
self.Animations.Idle:Play(.2)
self.AmmoLabel.Text = `{self.CurrentAmmo.Value}/{self.MaxAmmo}`
self.BulletsGUI.Enabled = true
self:_crosshairEnabled(true)
ContextActionService:BindAction("Reload",
function(...) return self:_reload(...) end,
true,
Enum.KeyCode.R,
Enum.KeyCode.ButtonY
)
end)
self.Tool.Unequipped:Connect(function()
self:_unequip()
end)
GunEvent.Unequip.listen(function()
GunClient:_unequip()
end)
self.Tool.Destroying:Connect(function()
print("Destroying")
end)
self.Tool:GetPropertyChangedSignal("Parent"):Connect(function()
print("Parent changed")
if self.Tool.Parent == nil then
print("tool parent nil")
end
end)
self.Tool.AncestryChanged:Connect(function(_, parent: Instance)
print("ancestry changed: ", _, parent)
if not parent then
print("ancestry nil")
self:_unequip()
self:Destroy()
end
end)
-- below here it's just normal connections, nothing wrong
if self.Automatic then
self.Tool.Activated:Connect(function()
self.Holding = true
end)
self.Tool.Deactivated:Connect(function()
self.Holding = false
end)
table.insert(self._connections,
RunService.RenderStepped:Connect(function()
if self.IsEquipped and self.Holding then
self:Shot()
end
end)
)
else
self.Tool.Activated:Connect(function()
self:Shot()
end)
end
--GUN EVENTS
GunEvent.onHit.listen(function(data)
local hum = data.hum
Fns.highlightHit(hum)
PlaySound(self.SFX.Parent.Hitmarker, self.Tool)
end)
GunEvent.Reloaded.listen(function(data)
self.CanFire = data.canShoot
self.IsReloading = not data.canShoot
if self.IsReloading and self.IsEquipped then
PlaySound(self.SFX.Reload, self.Tool)
end
end)
--CASTER
self.Caster.LengthChanged:Connect(function(_, lastPoint, dir, length, _, bullet)
local half = bullet.Size.Z / 2
local offset = CFrame.new(0, 0, -(length - half))
bullet.CFrame = CFrame.new(lastPoint, lastPoint + dir):ToWorldSpace(offset)
end)
self.Caster.RayHit:Connect(function(_,result: RaycastResult,_,bullet)
Fns.createBulletHole(result.Position, result.Normal, result.Instance)
bullet:Destroy()
end)
--MISC
self.CurrentAmmo.Changed:Connect(function()
if self.IsEquipped then
self.AmmoLabel.Text = `{self.CurrentAmmo.Value}/{self.MaxAmmo}`
end
end)
end
Here is what it looks like:
I’ve asked AI chats, searched online, found nothing.
Thank you.