PathFindingService stuttering

Hello, when using PathFindingService any ai just stutters and has really bad reaction time.

I tried using SetNetworkOwner() but it didnt work.

Script originally made by elite_hunter11 but modified by me:

local PathfindingService = game:GetService("PathfindingService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local hitbox = character:WaitForChild("Hitbox")

local walk = character.Walk
local attack = character.Attack

walk = humanoid:LoadAnimation(walk)

attack = humanoid:LoadAnimation(attack)

walk:Play()

function getClosestPlayer()
	local Players = game.Players:GetPlayers()
	local nearestPlayer = Players[1]

	if nearestPlayer == nil then return end

	if #Players > 1 then
		for i = 2, #Players do
			if nearestPlayer.Character == nil then
				nearestPlayer.CharacterAdded:Wait()
			end
			if Players[1].Character == nil then
				Players[1].CharacterAdded:Wait()
			end

			local distance1 = (nearestPlayer.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude
			local distance2 = (Players[i].Character.HumanoidRootPart.Position - rootPart.Position).Magnitude

			if distance1 > distance2 then
				nearestPlayer = Players[i]
			end
		end
	end

	return nearestPlayer
end

local path = PathfindingService:CreatePath({
	["AgentCanJump"] = false,
})

coroutine.wrap(function()
	while wait() do
		local closestPlayer = getClosestPlayer()
		
		if closestPlayer.Character == nil then
			closestPlayer.CharacterAdded:Wait()
		end
		
		path:ComputeAsync(rootPart.Position, closestPlayer.Character.HumanoidRootPart.Position)
		
		local waypoints = path:GetWaypoints()
		
		for _, waypoint in pairs(waypoints) do
			if closestPlayer.Character.Humanoid.Health > 0 then
				humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
			end
			
		end
	end
end)()

local debounce = false

while wait() do
	local found = false
	
	local region = Region3.new(hitbox.Position - (hitbox.Size / 2), hitbox.Position + (hitbox.Size / 2))
	
	local parts = game.Workspace:FindPartsInRegion3WithIgnoreList(region, script.Parent:GetDescendants())
	
	local model
	
	for _, part in pairs(parts) do
		model = part:FindFirstAncestorOfClass("Model")
		
		if model then
			if model:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(model)
				if player then
					found = true
					break
				end
			end
		end
	end
	
	if found then
		if not debounce then
			model.Humanoid.Health -= 10
			attack:Play()
			debounce = true
			wait(1)
			debounce = false
		end
	end
end

Note: I just noticed that the video is really laggy for some reason, but I think you still get whats the problem.

In The Video, you cant tell whats going on, just a few frames


Have you tried using RunService.Hearbeat or RunService.Stepped?

Yes I realised that after I posted it.

Also no I will try that, thanks.

I tried to record it but roblox studio crashed lol.

Anyways I tried heartbeat and it appeared smoother but the ai was having a seizure for some reason.
I replaced the while wait() do with runservice.heartbeat:Connect(function()

This is a server script so I cant use renderstepped.

There is a Difference between RenderStepped and Stepped as they are two different connections

After trying stepped it still had a seizure but in the other direction for some reason.

Tho I did keep the while wait() do and replaced this part which seems to have made it a bit smoother but now it also twitches.

for _, waypoint in pairs(waypoints) do
				if closestPlayer.Character.Humanoid.Health > 0 then
					humanoid:MoveTo(waypoint.Position)
					repeat 
						distance = (waypoint.Position - humanoid.Parent.PrimaryPart.Position).magnitude
					wait()
					until
					distance <= 5
				end

			end

What is the difference?? I’m experimenting stuff with RunService and I really don’t know the difference between Heartbeat, RenderStepped, and Stepped.

RenderStepped runs Prior to (Before) the frame being Rendered

Stepped runs Prior to (Before) the Physics Simulation

Hearbeat runs Post (After the) Physics Simulation, an alternative would be with task.wait()

There is Documentation on this Subject: