Pathfinding NPC super choppy?

Hello developers, I’ve been experimenting with path finding for the first time today but found a pretty big issue. The movement gets like really choppy and it lags a lot for some reason, heres some footage:
(https://gyazo.com/855025f37383b121007b4a890fed935b)

And this is my code:


local StartingPoint = script.Parent.PrimaryPart
local humanoid = script.Parent.Humanoid
local root = script.Parent.HumanoidRootPart
local smallest, enemy = math.huge, nil
local Path:Path
Path = pathfindingService:CreatePath()


function getNearestEnemy()
	smallest = math.huge --Reset Smallest
	local enemyFound = false
	for _, otherCharacter in pairs(workspace:GetChildren()) do
		if otherCharacter:FindFirstChild("HumanoidRootPart") and otherCharacter ~= script.Parent then
			local distance = (root.Position - otherCharacter.HumanoidRootPart.Position).Magnitude
			if distance < 60 and distance < smallest then
				smallest = distance
				enemy = otherCharacter
				enemyFound = true
			end	
		end
	end
	if not enemyFound then enemy = nil end
end

local function WalkHumanoid(humanoid:Humanoid, startGoal:Vector3, endGoal:Vector3)
	repeat Path:ComputeAsync(startGoal,endGoal) until Path.Status == Enum.PathStatus.Success

	local WayPoints = Path:GetWaypoints()
	local CurrectWayPointIndex = 2 --the point to go after the first point as the first waypoint is just the start position

	local MoveToFinishConnection:RBXScriptConnection
	local PathBlockedConnection:RBXScriptConnection

	PathBlockedConnection = Path.Blocked:Connect(function(blockedWaypointIndex)
		warn(blockedWaypointIndex,CurrectWayPointIndex)
		if blockedWaypointIndex >= CurrectWayPointIndex then--a waypoint ahead of us is blocked! recompute the path
			--disconnect the events
			MoveToFinishConnection:Disconnect()
			PathBlockedConnection:Disconnect()

			humanoid.WalkToPoint = StartingPoint.Position --make our humanoid stop walking at its current position and cancel it's movement to the next waypoint
			WalkHumanoid(humanoid,StartingPoint.Position,endGoal)

		end
	end)

	MoveToFinishConnection = humanoid.MoveToFinished:Connect(function(reached)
		if reached then--if the humanoid reach the waypoint within 8 seconds
			if CurrectWayPointIndex < #WayPoints then--if we have not cycle through the last waypoint
				CurrectWayPointIndex += 1--we increase the index by 1 so that the humanoid moves to the next waypoint
				humanoid:MoveTo(WayPoints[CurrectWayPointIndex].Position)--and the same thing
				if WayPoints[CurrectWayPointIndex].Action == Enum.PathWaypointAction.Jump then
					humanoid.Jump = true
				end
			else
				print("Path reached!")
				MoveToFinishConnection:Disconnect()
				PathBlockedConnection:Disconnect()
			end
		else--failed to reach waypoint within 8 seconds, the dummy probably is stuck, so let's recompute it!

			MoveToFinishConnection:Disconnect()
			PathBlockedConnection:Disconnect()
		end
	end)
	humanoid:MoveTo(WayPoints[CurrectWayPointIndex].Position)
	if WayPoints[CurrectWayPointIndex].Action == Enum.PathWaypointAction.Jump then
		humanoid.Jump = true
	end
end

while true do
	task.wait(0.5)
	getNearestEnemy()
	if enemy then
		WalkHumanoid(humanoid, StartingPoint.Position, enemy.HumanoidRootPart.Position)
	end
end	

Any help is appreciated, thanks!

You can either Apply Network Ownership like so:

NPC.PrimaryPart:SetNetworkOwner(nil)

Which may help inprove it, or you can use RunService.Heartbeat or RunService.Stepped to have them run faster.

However, from what Ive seen Humanoid.MoveToFinished:Wait() could also be the culprit.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.