Pathfinding Stuttering when Near Player

I’m encountering an issue for my civilian AI where if the player gets close to the character, they start stuttering in their movements.

Any help will be greatly appreciated!!

Script:

local pathfindingService = game:GetService("PathfindingService")
local pathsetDecided = false
local movementDebounce = false
local chosenPathset = nil
local pathNumber = 0

while true do
	if pathsetDecided == true and chosenPathset ~= nil then
		if movementDebounce == false then
			movementDebounce = true
			pathNumber = pathNumber + 1
			if #chosenPathset:GetChildren() < pathNumber then
				local civSystem = game.ReplicatedStorage.SpawnedCivs:FindFirstChild(script.Parent.Parent.Name)
				if civSystem then
					civSystem.Parent = game.ReplicatedStorage.Civilians
				end
				script.Parent.Parent:Destroy()
				break
			end
			local chosenPath = chosenPathset:FindFirstChild("waypoint"..pathNumber)
			if ("waypoint"..#chosenPathset:GetChildren()) >= chosenPath.Name then
				local path = pathfindingService:CreatePath()
				path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position, chosenPath.Position)
				for i, waypoint in pairs(path:GetWaypoints()) do
					script.Parent.Parent.Humanoid:MoveTo(waypoint.Position)
					script.Parent.Parent.Humanoid.MoveToFinished:Wait()
					movementDebounce = false
				end
			end
		end
	else
		local pathChoices = game.Workspace.CivWaypoints:GetChildren()
		local chosenPathNumber = math.random(1, #pathChoices)
		chosenPathset = pathChoices[chosenPathNumber]
		script.Parent.Parent.HumanoidRootPart.CFrame = chosenPathset.waypoint1.CFrame
		pathsetDecided = true
	end
	wait(0.1)
end
local pathfindingService = game:GetService("PathfindingService")
local pathsetDecided = false
local movementDebounce = false
local chosenPathset = nil
local pathNumber = 0

while true do
	if pathsetDecided == true and chosenPathset ~= nil then
		if movementDebounce == false then
			movementDebounce = true
			pathNumber = pathNumber + 1
			if #chosenPathset:GetChildren() < pathNumber then
				local civSystem = game.ReplicatedStorage.SpawnedCivs:FindFirstChild(script.Parent.Parent.Name)
				if civSystem then
					civSystem.Parent = game.ReplicatedStorage.Civilians
				end
				script.Parent.Parent:Destroy()
				break
			end
			local chosenPath = chosenPathset:FindFirstChild("waypoint"..pathNumber)
			if ("waypoint"..#chosenPathset:GetChildren()) >= chosenPath.Name then
				local path = pathfindingService:CreatePath()
				path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position, chosenPath.Position)
				for i, waypoint in pairs(path:GetWaypoints()) do
					script.Parent.Parent.Humanoid:MoveTo(waypoint.Position)
					script.Parent.Parent.Humanoid.MoveToFinished:Wait()
					movementDebounce = false
				end
			end
		end
	else
		local pathChoices = game.Workspace.CivWaypoints:GetChildren()
		local chosenPathNumber = math.random(1, #pathChoices)
		chosenPathset = pathChoices[chosenPathNumber]
		script.Parent.Parent.HumanoidRootPart.CFrame = chosenPathset.waypoint1.CFrame
		pathsetDecided = true
	end
	wait(0.1)
end

(I tried to put a video up yet it kept failing to upload sadly)

1 Like

Are you setting network ownership on the NPC?

NPCcharacter:SetNetworkOwner(nil)

Do that on the PrimaryPart and all should be OK. Without setting it, the Client and Server will be fighting for ownership of the NPC physics.

1 Like

Ohhhhh, noted. I’ll give that a shot

That worked! Thank you so much.

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