NPC Pathfinding Script makes the npc stutter walk

I am currently using GnomeCode’s pathfinding ai from his game Teddy. It works but the npc stutters a lot.

local teddy = script.Parent
local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = teddy.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, teddy)
	
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")
	
	local pathParams = {
		["AgentHeight"] = 15,
		["AgentRadius"] = 4,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 8 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		target.Humanoid.Health = 0
	end
end

local function walkTo(destination)
	
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				print("TARGET FOUND", target.Name)
				attack(target)
				break
			else
				print("Moving to ", waypoint.Position)
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.25) do
	patrol()
end


Any help?

9 Likes

robloxapp-20250106-1520247.wmv (2.1 MB)

I would assume it has something to do with your animation code

yea it is the humanoidrootpart

i dont think it is cuz when it starts chasing me, the animation runs fine. but when its at the pathfinding part it stutters

Had a similar problem to this and the solution was that the NPC was under player network ownership and I needed to make it have server network ownership

1 Like

hmm, maybe it is an animation thing lemme check

ok is there like special things u did with ur animations or something?

hmm idk what to fix abt my animation, it could be my custom character but idk

default animations script with edited ids

uhhhh sure.
untitled game by techno_blazes.rbxl (156.5 KB)

idk why but it still stutters when he walks

Is the animation your playing looped?

it was but now its not looped anymore

it removed it but it still stutters

Ok so i added “WaypointSpacing” and it works now?!

local pathParams = {
		["AgentHeight"] = 13,
		["AgentRadius"] = 4,
		["AgentCanJump"] = false,
		["WaypointSpacing"] = 20
	}
1 Like

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