PathFinding monster that wont stop and stuck

Hello there! I want to make monster that will found you in any location.
I made this script:

plr = workspace:WaitForChild('pupersuperkirill')
pathf = game:GetService("PathfindingService")

pathf = game:GetService("PathfindingService")

function l(n1,n2)
	if (n1.Position - n2.Position).magnitude > 2.5 then
		return true
	end
end

function getpoint(points)
	
	for q,i in pairs(points) do

		if points[q+1] and points[q+2] and l(points[q],points[q+1]) then
			point = points[q+1]
			return point
		end
		
	end
	return points[2]
end

while true do
	path = pathf:CreatePath{
		AgentRadius = script.Parent:GetExtentsSize().X-1.5,
		AgentHeight = script.Parent:GetExtentsSize().Y-1.5,
		AgentCanJump = true,
		WaypointSpacing = 2.5

		--Costs = {
		--	Grass = 10
		--}
	}

	path:ComputeAsync(script.Parent.PrimaryPart.Position,plr.PrimaryPart.Position)
	points = path:GetWaypoints()
	--for _, i in pairs(points) do
	point = getpoint(points)
	--if (point[1].Position - points[2].Position).magnitude < 2 then
	--	point = points[3]
	--end
	if point then
		if point.Action == Enum.PathWaypointAction.Jump and script.Parent.Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		pos = point.Position
		script.Parent.Humanoid:MoveTo(pos)
	end
	--end


end

But npc sometimes stucks,goes with little stops.
Do you have any script or recomendation how to make a clever monster like piggy,scp 3008 A in scp 3008?

you’re overcomplicating it, if you loop the pathfinding the monster will take too long to where to actually go and also get lost, every time you pathfind its gonna basically go in a process to ‘find the path’ thus when it steps it usually means its gonna have to find another path to take as I said you looped it, I recommend only using pathfind when the said target is either not found or not reachable. else just use MoveTo if the target is infront of it with no obstacles. Anyways what’s happening is basically the monster pathfinds then after that it moves for a bit then find another path to take according to that new position repeat, thus explains why it moves for a bit then stops.

1 Like