How would i make it to chase a player and then continue the pathfinding

local npc = game.Workspace.teddy

while true do
	wait(0)
	npc.Humanoid:MoveTo(game.Workspace.Part1.Position)
	npc.Humanoid.MoveToFinished:Wait()
	wait(0)
	npc.Humanoid:MoveTo(game.Workspace.Part2.Position)
	npc.Humanoid.MoveToFinished:Wait()
	wait(0)
	npc.Humanoid:MoveTo(game.Workspace.Part3.Position)
	npc.Humanoid.MoveToFinished:Wait()
	
end

How would i make it to chase a player and then still continue the pathfinding.

What exactly are making here? I know what you want to do. But do you want to chase a player within a certain range?

If you just get near him. The npc will attacks you. And will continue the pathfinding after killing.

Try putting this into a function and make it call itself at the end. This will make it so the function will run automatically after it is complete. Just make sure you put a couple of waits in there so it doesn’t crash your computer.

Something like this:

local npc = game.Workspace.teddy
local findplayer()
	wait(0)
	npc.Humanoid:MoveTo(game.Workspace.Part1.Position)
	npc.Humanoid.MoveToFinished:Wait()
	wait(0)
	npc.Humanoid:MoveTo(game.Workspace.Part2.Position)
	npc.Humanoid.MoveToFinished:Wait()
	wait(0)
	npc.Humanoid:MoveTo(game.Workspace.Part3.Position)
	npc.Humanoid.MoveToFinished:Wait()
	findplayer() -- Repeats the cycle
end

findplayer() -- Starts the cycle
1 Like

Doesn’t seems to work.
Capture d’écran 2022-01-14 194426

you got to do function findplayer() when you do local findplayer() you’re making a variable, so you need to make it into a function in order to make it function properly. Example:

function killplayer()
plr.Character.Humanoid.Health -= 10
end)
killplayer()

functions run the lines inside the function, and you call the function in order to make it run the lines of code inside the function.

1 Like

Could you try explaining , I don’t really understand. ?

First, I would have your path be in a function so you can call it. You can spawn a loop in the function to detect the players using magnitude then redirect the humanoid to move towards the player. Once it is finished, you can break the loop and call the function after to restart the process if that makes sense.

Like that?

local npc = game.Workspace.teddy
local function killplayer()
wait(0)
npc.Humanoid:MoveTo(game.Workspace.Part1.Position)
npc.Humanoid.MoveToFinished:Wait()
wait(0)
npc.Humanoid:MoveTo(game.Workspace.Part2.Position)
npc.Humanoid.MoveToFinished:Wait()
wait(0)
npc.Humanoid:MoveTo(game.Workspace.Part3.Position)
npc.Humanoid.MoveToFinished:Wait()

local function killplayer()
	plr.Character.Humanoid.Health -= 10
end)
killplayer()
3 Likes

Yes, that’s how you should do it.

Doesn’t seems to work too. There is no errors in the output.

1 Like

remove the local im really sorry. I forgot about that

1 Like

Could you clarify what your current script is doing? By the looks of it, it just makes the NPC move to 3 different parts in the game on a loop. Is this correct?

You can do something like this:

local npc = game.Workspace.teddy
local chaseDistance = 50 --How close they have to be to get chased (in studs)

local currentPartPos = 1

local function GetNearPlayers()
       local closestPlayer
       local closestPos = chaseDistance

       for i,v in pairs(game.Players:GetPlayers())do
             if v.Character then
                   if (v.Character.HumanoidRootPart.Position-npc.HumanoidRootPart.Position).Magnitude < closestPos then
                         closestPlayer = v
                   end
            end
      end

      return closestPlayer
end

while true do
    task.wait() --We use task.wait() because regular wait() is becoming deprecated.

    local NearestPlayer = GetNearPlayers()

    if NearestPlayer then
         npc.Humanoid:MoveTo(NearestPlayer.Character.HumanoidRootPart.Position)
         npc.Humanoid.MoveToFinished:Wait()
   else
         npc.Humanoid:MoveTo(game.Workspace["Part"..currentPartPos].Position)
         npc.Humanoid.MoveToFinished:Wait()
         currentPartPos += 1

         if currentPartPos == 4 then
              currentPartPos = 1
         end
   end
end

Let me know if you have any issues!

If you have any questions on how this works, let me know as well.

2 Likes

Do i still need to name the parts?

Yes. And if you want more than 3 parts, you have to change the line at the bottom that says “if currentPartPos == 4” and change the 4 to your number of parts +1. Name all the parts like “Part1, Part2, etc.”

1 Like

brother, I’m sorry, but your script gave me sore eyes, it was like a punishment; anyone gave you the solution but your script had errors when you closed the function