Monsters seem to be "dead" but still sometimes attacking

Randomly on some servers for my game “Town Defend” several of the monsters seem to be attacking even post-death.

For context, it seems that the sounds do not repeat, however, randomly my orc will still be attacking after death.
It doesn’t always happen, it seems to only occur once in a while. The code is huge, and has multiple sections but if anyone has an idea why there’d be “ghost” monsters within my game “sometimes” what should I do?

I have checked and I can almost confirm it has to be a ghost orc attacking because that’s the only thing within my game with a TakeDamage function.


                canDamage = distance <= 5

                if (canDamage) then

                    self.LastDamage = current

                    if self.Humanoid.Health > 0 then

                        closestPlayer.humanoid:TakeDamage(self.Damage)

                        SoundHandler.PlaySound("Orc.attack", rig, {

                            Volume = 0.8;

                            PlaybackSpeed = math.random(800, 1200)/1000;

                        })

                        self.WalkAnimation:Stop()

                        self.DanceAnimation:Stop()

                        self.AttackAnimation:Play()

                    end

I thought I patched this bug several times, but rejoining on some servers I’ve found it again :frowning:

Update:
It’s only on older servers, the longer the server, the more likely the bug occurs.

If this script is all in a loop, check to see if the monster’s health is at zero, return their health, and then break the loop