NPC breaks after killing player

So, I tried making a NPC script that attacks you if you get too close, and I managed to do it! There’s just one problem though… after the NPC kills me it just breaks and stays on the position it killed me and keeps punching the air, and even if I get on its damage range it doesnt damage, as well as if I push it (NPC) away it just walks back to where it killed me and continues punching the air.

I’ve tried to place

if char.Humanoid.Health <= 0 then
target = nil
script.StopAttackRemote:Fire()
else if char.Humanoid.Health > 1 or char.Humanoid.Health == 1 then
target = char
end
end

and it still didn’t work. Tried placing

script.Parent.StopAttackRemote.Event:Connect(function()

for i,v in pairs(game.Players:GetChildren()) do

  	if not game.Workspace:FindFirstChild(v.Name) then continue end
  	local player = game.Workspace[v.Name]
  	
  	if player.Humanoid.Health <= 0 then
  		hum.WalkSpeed = originalWalkSpeed
  		canAttack = false
  	end
  	
  	
  end

end)

on its attack script and still didn’t work. I’m pretty confused on how to fix it, and some videos on youtube haven’t helped either.
The scripts I made are below.
Main script:

local hum = script.Parent:WaitForChild(“Humanoid”)
local root = script.Parent:WaitForChild(“HumanoidRootPart”)

local RunService = game:GetService(“RunService”)

local visionRange = 10
local attackRange = 3
local abandonRange = 20

local target = nil

RunService.Heartbeat:Connect(function()
if target then
local plrRoot = target.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude

  hum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackRange)
  
  if distance <= attackRange + 2 then
  	script.AttackRemote:Fire(plrRoot)
  end
  
  if distance > abandonRange then
  	target = nil
  end

else
for i,v in pairs(game.Players:GetChildren()) do

  	if not game.Workspace:FindFirstChild(v.Name) then continue end
  	local char = game.Workspace[v.Name]
  	local plrRoot = char.HumanoidRootPart
  	
  	local distance = (root.Position - plrRoot.Position).magnitude
  	
  	if char.Humanoid.Health <= 0 then
  		target = nil
  		script.StopAttackRemote:Fire()
  	else if char.Humanoid.Health > 1 or char.Humanoid.Health == 1 then
  			target = char
  		end
  	end
  	
  	if distance < visionRange then
  		target = char
  	end
  	

  end

end

end)

Attack script:

local attackCooldown = 1
local attackWindup = 1
local attackRange = 5
local attackDamage = 35

local char = script.Parent.Parent
local hum = char:WaitForChild(“Humanoid”)
local originalWalkSpeed = hum.WalkSpeed

local canAttack = true

script.Parent.AttackRemote.Event:Connect(function(plrRoot)

if not canAttack then return end
canAttack = false

hum.WalkSpeed = 0.1

local anim = Instance.new(“Animation”)
anim.AnimationId = “rbxassetid://6777953841”
local playAnim = hum:LoadAnimation(anim)
playAnim:Play()

wait(attackWindup)
local newDistance = (char.HumanoidRootPart.Position - plrRoot.Position).magnitude

if newDistance <= attackRange + 5 then

  plrRoot.Parent.Humanoid:TakeDamage(attackDamage)

end

wait(attackCooldown)
hum.WalkSpeed = originalWalkSpeed
canAttack = true

script.Parent.StopAttackRemote.Event:Connect(function()

  for i,v in pairs(game.Players:GetChildren()) do

  	if not game.Workspace:FindFirstChild(v.Name) then continue end
  	local player = game.Workspace[v.Name]
  	
  	if player.Humanoid.Health <= 0 then
  		hum.WalkSpeed = originalWalkSpeed
  		canAttack = false
  	end
  	
  	
  end

end)
end)

If anyone can help I’d be extremely grateful since Im a bit bad at scripting! :sweat_smile:

(Reposting since last one was barely barely barely BARELY seen)

2 Likes

Are you getting any errors on the Output at all?

1 Like

nope, nothing pops out on the output

1 Like

and once again it went quiet, sigh

Sorry I easily forget about these posts

Have you tried printing what outputs & outputs back? Supposedly in the Attack Script when you call this conditional statement:

if newDistance <= attackRange + 5 then
1 Like

yeah I fixed this, I just had to remove the stopattack remote and remove the second canAttack = false in the attack script, as well as switch the main RunService for this

RunService.Heartbeat:Connect(function()
if target then
local plrRoot = target.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude

    hum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackRange)

    if distance <= attackRange + 2 and target.Humanoid.Health >= 1 then
        script.AttackRemote:Fire(plrRoot)
    elseif target.Humanoid.Health <= 1 then
        target = nil
    end

if distance > abandonRange then
target = nil
end
else
for i,v in pairs(game.Players:GetChildren()) do

        if not game.Workspace:FindFirstChild(v.Name) then continue end
        local char = game.Workspace[v.Name]
        local plrRoot = char.HumanoidRootPart

        local distance = (root.Position - plrRoot.Position).magnitude

        if char.Humanoid.Health <= 0 then
            target = nil
            script.StopAttackRemote:Fire()
        else if char.Humanoid.Health > 1 or char.Humanoid.Health == 1 then
                target = char
            end
        end

        if distance < visionRange then
            target = char
        end


    end

but thanks for the help! really appreciated!