NPC Studdering when near player even though walkspeed is higher

Hello!

Ive been working on a pathfind npc, (while theres been two problems, im deciding to make a post on this one for the pure fact that my other one got ghosted) and the npc kinda… freaks out when they are near the player. This is what is happening:

Heres my code:

task.wait(4)

local ai = script.Parent

local humanoid = ai:WaitForChild("Humanoid")
local torso = ai:WaitForChild("Torso")
local hrp = ai:WaitForChild("HumanoidRootPart")

local pathfind_service = game:GetService("PathfindingService")
local path = pathfind_service:CreatePath({
	AgentRadius = 1.5;
	AgentHeight = 6;
	AgentCanJump = true;
	Costs = {
		Climb = 2;
	}
	
})

local waypoints
local next_index
local reached
local blocked

local visualize = true
local visualized_waypoints = {}

ai.PrimaryPart:SetNetworkOwner(nil)

function follow_path(destination)
	local success, error_mes = pcall(function()
		path:ComputeAsync(hrp.Position, destination)
	end)
	
	if visualize then
		for i, v in ipairs(visualized_waypoints) do
			v:Destroy()
		end

		table.clear(visualized_waypoints)
	end
	
	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		blocked = path.Blocked:Connect(function(index)
			if index > next_index then
				blocked:Disconnect()
				follow_path(destination)
			end
		end)
		
		if visualize then
			for i, waypoint in pairs(waypoints) do
				local part = Instance.new("Part")
				
				part.Anchored = true
				part.CanCollide = false
				part.Parent = workspace
				part.Position = waypoint.Position
				
				part.Shape = Enum.PartType.Ball
				part.Material = Enum.Material.Neon
				part.Size = Vector3.new(0.5, 0.5, 0.5)
				part.Color = Color3.fromRGB(255, 255, 255)
				
				table.insert(visualized_waypoints, part)
			end
		end
		
		if not reached then
			reached = humanoid.MoveToFinished:Connect(function(reached_)
				if reached_ and next_index < #waypoints then
					next_index += 1
					humanoid:MoveTo(waypoints[next_index].Position)
					
					if waypoints[next_index].Action == Enum.PathWaypointAction.Jump then
						humanoid.Jump = true
					end
				else
					reached:Disconnect()
					blocked:Disconnect()
				end
			end)
		end 
		
		next_index = 2
		humanoid:MoveTo(waypoints[next_index].Position)
		if waypoints[next_index].Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end
	else
		warn(error_mes)
	end
end

while task.wait() do
	follow_path(workspace.scpSCARRYY.Other.LocalCharacterPos.Value.Position)
end
2 Likes

Dont wanna hear anything about “set network owner to nil”, “make npc follow the CLIENTS position using rmeote events waaa” because ive already tried all of those and it doesnt work.

So as doing a magnitude check for the distance between waypoints. NOTHING is working.

1 Like

The NPC’s behavior could be caused by the path being recomputed too frequently or the NPC getting stuck on obstacles. Try adding a check to see if the player has moved a certain distance before recomputing the path and adjust the AgentRadius , AgentHeight , and Climb properties of the Path object to better match the size of your NPC.

1 Like

Changed the tick speed of the calculations. Problem got worse.

Changed agent radius, problem still persisted.
Changes agent height, problem still persisted.
Deleted truss cost, problem still persisted.
Turned off visualize waypoints, problem still persisted.

1 Like

Try adding debugging statements to your code to see if there are any issues with the pathfinding calculations or the NPC’s movement

npcs movement debugging:

print(destination)
print(workspace.scpSCARRYY.Other.LocalCharacterPos.Value.Position)

output:

  21:25:17.015  -11.619033813476562, 0, 116.34712982177734  -  Server - Script:93
  21:25:17.015  -11.82643985748291, 2.9999992847442627, 117.52330780029297

seems like theres a bit of difference. How would I fix it though?

Try adjusting the NPC’s movement speed, AgentRadius , and AgentHeight properties of the PathfindingService to improve its accuracy in reaching its target

Pathfind works fine. But the problem is that if the npc is TOO fast then it will studder.

add smoothing to its movement using a TweenService

? How would I do that? ‎ ‎ ‎ ‎

sorry for poor code, kinda busy rn:

local tweenService = game:GetService("TweenService")

-- Inside your follow_path function, after you get the next waypoint:
local nextWaypoint = waypoints[next_index].Position
local distance = (nextWaypoint - hrp.Position).Magnitude
local time = distance / humanoid.WalkSpeed

local tweenInfo = TweenInfo.new(time)
local tween = tweenService:Create(hrp, tweenInfo, {CFrame = CFrame.new(nextWaypoint)})
tween:Play()

I dont wanna use CFrames for my pathfind. Hence the “Humanoid:MoveTo()”

What is the line below referring to?

1 Like

??? My position?? What else would it refer to?

Why not just use the HumanoidRootPart.Position instead of a custom value?

Because it would lag behind even more if I used the position from the servers perspective.