How can I Make that whenever you attack an npc it chase the player that attacked him

ok so I have this script for the chase and damage system


And I have this in the sword script
– Variables

local Tool = script.Parent
local players = game:GetService(“Players”)

– Variables
local Tool = script.Parent
local Handle = Tool:WaitForChild(“Handle”)
local Model = Tool:WaitForChild(“Model”)
local damagegui = game.ServerStorage.BillboardGui

local Slash = script:WaitForChild(“Slash”)
local Debounce = false
local PlayersHit = {}

local slashsound = script:WaitForChild(“SlashSound”)
local swingSound = script:WaitForChild(“SwingSound”)

local damage = 10

– Adds Welds
for i, Parts in pairs(Model:GetChildren()) do
if Parts:IsA(“BasePart”) then
local Weld = Instance.new(“WeldConstraint”)
Weld.Part0 = Parts
Weld.Part1 = Handle
Weld.Parent = Parts
end
end

– Animate
Tool.Activated:Connect(function()
if Debounce == false then
Debounce = true
swingSound:Play()
local Humanoid = Tool.Parent:WaitForChild(“Humanoid”)
local AnimTrack = Humanoid:LoadAnimation(Slash)
AnimTrack:Play()
task.wait(1)
Debounce = false
end
end)

– Damage
Handle.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild(“Humanoid”) and Hit.Parent ~= Tool.Parent and not players:GetPlayerFromCharacter(Hit.Parent) then
if Hit.Parent:FindFirstChild(“Humanoid”).Health > 0 then
if Debounce == true and PlayersHit[Hit.Parent] == nil then
Hit.Parent:FindFirstChild(“Humanoid”):TakeDamage(damage)
slashsound:Play()
local cloneGui = damagegui:Clone()
PlayersHit[Hit.Parent] = true
cloneGui.Parent = Hit.Parent.Head
cloneGui.TheShadow.Text = damage
wait(.3)
cloneGui:Destroy()
task.wait(1)
PlayersHit[Hit.Parent] = nil
end
end
end
end)

With those scripts how can i make that when the player attacks the monster the monster like gets angry and chases the player that attacked him

1 Like

image
add this code after local AttackSound = script.Ragh

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      repeat task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
       until nowPlr ~= player
end

image
add this code after debounce = true

elseif hit.Name == "Handle" and Players:GetPlayerFromCharacter(hit.Parent.Parent) ~= nil and hit.Parent.Parent:FindFirstChild("Humanoid").Health > 0 then
      player = Players:GetPlayerFromCharacter(hit.Parent.Parent) 
      nowPlr = player
      StartToChaseThisPlayer(player)
end

and delete the while true loop.

1 Like

I saw you replying and I was so exited for you answer lol. Thanks let me test it

1 Like

Ok so i did what you said but it says that nowplayer is wrong
image
even though let me test it

Yes it works! thank you so much

hey but is there any chance that you can like make him chase the player that punched him for ever until the player is dead or the player kills the npc?
because it only like punches the player 1 time and thats it

Sorry, i don’t understand what you mean. Like chase player until he is dead or kills the npc or what?

yes thats what i mean. sorry if my grammar is bad

Okey, change this:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      repeat task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
       until nowPlr ~= player
end

To this:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      repeat task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
       until nowPlr ~= player or playerHumanoid.Health <= 0 or humanoid.Health <= 0
end

I think it dont works look what happens

Okey, try to change this:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      repeat task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
       until nowPlr ~= player or playerHumanoid.Health <= 0 or humanoid.Health <= 0
end

To this:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      while true do task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
      if  nowPlr ~= player or playerHumanoid.Health <= 0 or humanoid.Health <= 0 then
          break
         end
       end 
end

The same happens again, but why?


maybe because the monster just moves to the player once and not keeps following him maybe
im not sure

Can you check if HumanoidRootPart is anchored?

image

Then can you check if any body part is anchored?

image

Try then replace this script:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      while true do task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
      if  nowPlr ~= player or playerHumanoid.Health <= 0 or humanoid.Health <= 0 then
          break
         end
       end 
end

To this:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      while true do task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
      if  nowPlr ~= player then
          break
      elseif  playerHumanoid.Health <= 0 then 
             break
        elseif  humanoid.Health <= 0 then
              break
         end
       end 
end


Still not works :frowning:

Then try to change it to this:

local nowPlr = nil
local function StartToChaseThisPlayer(player)
      repeat task.wait() local playerCharacter = player.Character or player.CharacterAdded:Wait()
      local playerRoot = playerCharacter:WaitForChild("HumanoidRootPart")
      local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
       print(playerHumanoid)
      if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 and playerHumanoid then
       humanoid:MoveTo(playerRoot.Position)
       humanoid.MoveToFinished:Wait()
       end
       until nowPlr ~= player or playerHumanoid.Health <= 0 or humanoid.Health <= 0
end

And send me prints in output.

Still not works and it prints humanoid


now it brings me an error