How to make: Smart Chasing AI/NPC using PathFindingService

---------> Please Keep in mind that this is my first Thread :smile: <-------------

So What i am trying to create is a Smart NPC using PathFindingService

What i’m trying to do is make the path Reset Whenever the Part.Position Changes, And also for the npc to ignore the OLD generated path that doesn’t lead to the part ( because it already moved )
The Perfect example is Piggy Bot
So what i did that i tried to limit the AI movement to 2 steps ( 2 waypoints ) so it can compute and generate a new path

So here is my attempt:

--------------------- AI is in R6 -----------------------------
local PathFindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Torso = script.Parent:WaitForChild("Torso")
local EndingPoint = game.Workspace:WaitForChild("EndingPoint") --- this is just a part
local path = PathFindingService:CreatePath()
local WayPointsList = {}
local Limiter = 0 --- Number We gonna use to limit the path


wait(.5)
while true do ---- this is going to run every .2 Seconds
	wait(.2)
	path:ComputeAsync(Torso.Position,EndingPoint.Position) ---- Generating the path
	local waypoints = path:GetWaypoints() --- Getting the way points
	for i,Waypoint in pairs(waypoints) do ---- Looping through all the way points
		if Limiter < 4 then ---- if the number is equal or less than 2 its gonna add a waypoint to the list
			table.insert(WayPointsList,Waypoint) -- ading the waypoint
			Limiter = Limiter + 1 --- adding one num so we can limit
			print(Limiter)
		else ---- if the number is bigger than 2 then it will break
			break
		end
	end
	for i,inList in pairs(WayPointsList) do ---- looping through the List 
		if  Limiter == 4 then -- making sure the number is equal to 2
			Humanoid:MoveTo(inList.Position) --- moving the npc to the listed waypoint
			Limiter = 0 --- setting back the 
			table.remove(WayPointsList,1) --- removing it from the list
			Humanoid.MoveToFinished:Wait() --- waiting till the AI has reached the point
		else
			end
	end	
end

So What am i trying to do here is limit the Path So can the AI PROPERLY Reach/Follow the Part ( if the path isn’t limited it wont follow properly because the part is moving ) by limiting it that means it resets the current position of the part every 2 waypoints that the npc moved to and then generates a new path and new waypoints, so it can keep on track of the moving part

Why did i add a limter you may ask? because it limits the added Waypoints to the table

RESULTS: The AI/NPC kept on circling around itself… :rofl: … Well… that was a sad attempt and completly far from what i wanted

So uhhh that was my attempt, if you have a video/thread/Idea/Tuto that matches the goal Please reply and thanks :slight_smile:

3 Likes

I have a script that may work for you:

--Variables
local whilecountdown = 0
local pathservice = game:GetService("Pathfindingservice")
local path = pathservice:CreatePath()
local hum,hrp = script.Parent.Humanoid,script.Parent.HumanoidRootPart --2 variables at a single line
local EndingPoint = --case sensitive

--Path stuff
while wait() do
countdown = countdown + 1
path:ComputeAsync(hrp.Position,EndingPoint.Position)

for _,v in pairs(path:GetWaypoints()) do
hum:MoveTo(v)
hum.MoveToFinished:Wait()
end
if countdown == #path:GetWaypoints() then
break
end
end

Hope it helps!

1 Like

Thank You for the Help, I will test it out And reply :smiling_face_with_three_hearts:

Wait i forgot

Do hum:MoveTo(v.Position)

Yeah I know i noticed it, Thanks :slight_smile:

You can also make my post a solution so everyone knows it’s solved.

I’ve Edited up a bit, But its the same as my old attempts ( not the one that is mentioned in the thread )
What i’m trying to do is make the path Reset Whenever the Part.Position Changes, And also for the npc to ignore the OLD generated path that doesn’t lead to the part ( because it already moved )

Most Importantly Thank you for The help :smiling_face_with_three_hearts:

Ill help you :smiley: wait a second.

--Variables
local whilecountdown = 0
local pathservice = game:GetService("Pathfindingservice")
local path = pathservice:CreatePath()
local hum,hrp = script.Parent.Humanoid,script.Parent.HumanoidRootPart --2 variables at a single line
local EndingPoint = --case sensitive

--Path stuff
while wait() do
countdown = countdown + 1
path:ComputeAsync(hrp.Position,EndingPoint.Position)


EndingPart.PositionChanged:Connect(function() --when the part moves
local newPath = pathservice:CreatePath()
newPath:ComputeAsync(hrp.Position,EndingPart.Position)
for i,v in pairs(newPath:GetWaypoints()) do
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
end
end)



for _,v in pairs(path:GetWaypoints()) do
hum:MoveTo(v)
hum.MoveToFinished:Wait()
end
if countdown == #path:GetWaypoints() then
break
end
end

This might be the Solution… Testing :open_mouth:

1 Like

You can customize (like remove the while loop) if you want :slight_smile:

Output
PositionChanged is not a valid member of Part

Try this instead:
EndingPart.PositionChanged
to
EndingPart.Changed

Sorry if i’m bothering you :frowning:

1 Like

It works When the Part.Position is changed, But there is a slight issue, There is a kind of corruption because the part is constantly moving , The AI Goes back and forth many times

Here is the game file for you to test
E.rbxl (30.5 KB)
The script is placed in:
game > Workspace > NPC > PathFindingAI

Ill test it ill give a response/fix.

:+1: :smile: Thank you for your time

Firstly, why is the NPC not moving…?

Oh you have to move the part for it to trigger.

Move The Part First, All you need to do is Click Run And then move the part
Using Home > Move ( the Vector3 Arrows )