Pathfinding service is SLOW

I am making a simple simulator game that has enemy’s that you can kill in order to gain EXP and Money. Problem is, Pathfinding Service is slow, and when I try to find a solution so that the pathfinding is smooth, its broken a different way. I am a new relatively new scripter but am pretty decent where I’m at.

Here in this video it shows how delayed the enemy is, I cannot find a solution for this and am seeking help. Video: https://gyazo.com/dc0d67a904cb9c64f7737ee8bde834a2

local pathFinding = game:GetService("PathfindingService")
local humanoid = script.Parent.Humanoid
local torso = script.Parent.UpperTorso
local FollowHB = script.Parent.Hitbox

local plrIsInHB = false
local zone = require(game.ReplicatedStorage.Modules.Zone)
local container = zone.new(FollowHB)

container.playerEntered:Connect(function(plr)
	plrIsInHB = true
	while true do
		if plrIsInHB == true then
			local currPath = pathFinding:CreatePath()
			currPath:ComputeAsync(torso.Position, plr.Character.Torso.Position)
			local waypoints = currPath:GetWaypoints()
			for i, waypoint in ipairs(waypoints) do
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				end
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
				if plrIsInHB == false then
					local currPath = pathFinding:CreatePath()
					currPath:ComputeAsync(torso.Position, workspace.StationaryPos1.Position)
					local waypoints = currPath:GetWaypoints()
					for i, waypoint in ipairs(waypoints) do
						if waypoint.Action == Enum.PathWaypointAction.Jump then
							humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						end
						humanoid:MoveTo(waypoint.Position)
						humanoid.MoveToFinished:Wait(.01)
					end
					break
				end
			end
		end
		task.wait(.008)
	end

end)

container.playerExited:Connect(function(plr)
	plrIsInHB = false
end)

Above this is my current code, I have tried this without the humanoid.MoveToFinished:wait(), although that fixes the delaying issue it jumps sporadically and I don’t know why. I could add a debounce to the jumping but i wanna know if there’s an alternative
Video: https://gyazo.com/a5db661e3c4d8244d17b6e191762590e

2 Likes
script.Parent.PrimaryPart:SetNetworkOwner(nil)

This might work…
Put this line of code below “local FollowHB = script.Parent.Hitbox

2 Likes

It didnt fix the jumping but made the walking a little smoother

There are some ways you can optimize pathfinding so it’s more efficient, one of the ways is to have a sort of ray (would be best if it was like a box formating thing) pointing from the npc to the character to check if there are any obstacles and if there are to use the pathfinding service, of course this depends on the sort of terrain your dealing with.

Something I see with your code is that you are doing :CreatePath() every time you want to compute a path which cost some performance this is not something that is ideal and you want to only run it once as the way your doing it is deleting and creating an object

local currPath = pathFinding:CreatePath()

container.playerEntered:Connect(function(plr)
	plrIsInHB = true
	while true do
		if plrIsInHB == true then
			currPath:ComputeAsync(torso.Position, plr.Character.Torso.Position)
			local waypoints = currPath:GetWaypoints()

Also another thing from your code is that I don’t see a reason to include this code?

				if plrIsInHB == false then
					local currPath = pathFinding:CreatePath()
					currPath:ComputeAsync(torso.Position, workspace.StationaryPos1.Position)
					local waypoints = currPath:GetWaypoints()
					for i, waypoint in ipairs(waypoints) do
						if waypoint.Action == Enum.PathWaypointAction.Jump then
							humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						end
						humanoid:MoveTo(waypoint.Position)
						humanoid.MoveToFinished:Wait(.01)
					end
					break
				end

if it’s still lagging behind you might want to make your own MoveToFinished

1 Like

the last part just moves the enemy back to its starting position.

1 Like