.Destroying event doesn't work or replicate

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.

Kinda a long shot but maybe, if the script initializing the class for the gun is under the gun, the script is getting destroyed before the gun gets destroyed.

send full code, I assume it’s only a part of it since I do not see you destroy the tool at any point in the code.

tip: if you can additionally avoid events such Destroying, do so:
assuming you have something like
self:destroy(), the method can simply handle everything that needs to happen, this method does not need to travel to other events for additional cleanup

There’s a very terrible ‘bug’ with deferred events that causes the .Destroying connection to disconnect before it can ever run if the script that created the connection is also destroyed.

If this is the case, try setting workspace.SignalBehavior to Immediate or AncestryDeferred. Alternatively, you could refactor your system so the script that runs this isn’t parented to the tool that gets destroyed.

1 Like

Thank you @iATtaCk_Noobsi, @index_self and @ChiDj123. It seems changing workspace.SignalBehavior from deferred to inmediate works, i will investigate how it affects performance and other things, anyways i’ll have to refactor my code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.