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.
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
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.
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.
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()
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?
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.
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.”
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