Pathfinding laggy/studdery

Hey, I scripted this path finding npc to follow the black path but for some reason its like studdering as you can see in this video:


I searched on the internet and searched devfurom but non of those solutions worked.

Here is my script:

Script

				local Npc = NpcClass.new(npc, self.Tycoon.Path.Start.CFrame)
					Npc.Init(Npc,player)

					Pathfinding.CreatePath(Npc, self.Tycoon.Path.One.P1.Position, {
						AgentCanJump = false,
						--WaypointSpacing = 5,
						Costs = {
							Path = math.huge
						}
					})

Module

function Pathfinding:CreatePath(Goal,PathInfo)
    local Path = PathfindingService:CreatePath(PathInfo)

    local Character = self.Character

    local success, err = pcall(function()
        Path:ComputeAsync(Character.PrimaryPart.Position,Goal)
    end)

    local Humanoid = Character:WaitForChild("Humanoid")

    if success and Path.Status == Enum.PathStatus.Success then
        local waypoints = Path:GetWaypoints()

        for _, waypoint in pairs(waypoints) do
            if waypoint.Action == Enum.PathWaypointAction.Walk then
                Humanoid:MoveTo(waypoint.Position)
                Humanoid.MoveToFinished:Wait()
            elseif waypoint.Action == Enum.PathWaypointAction.Jump then
                Humanoid.Jump = true
            end
        end
    else
        warn(err)
    end
end
2 Likes

Are you setting the NPC’s network owner to the server? It’s a common issue with NPCs.

1 Like

Tried doing that by doing this:

self.Character.PrimaryPart:SetNetworkOwnershipAuto()

I don’t know if this is the right way to do it.

But instead of helping me it made the npcs not even move they would just stand there doing nothing.

You should be calling it on every BasePart on every frame, like so:

RunService.Heartbeat:Connect(function()
    for _, v: BasePart in pairs(self.Character:GetDescendants()) do
        if v:IsA("BasePart") and v:CanSetNetworkOwnerShip() then
            v:SetNetworkOwner(nil)
        end
    end
)

You’re also calling the wrong function. You want to call SetNetworkOwner(nil). Hope this helps you out!

pathfinding is a bit laggy/buggy if run on a loop, to detect if it’s on a loop just print(“E”) or something to be 100% sure it’s not. If you want it to be on a loop you gotta be smart about it, and make like a wait(1) or an alternating follow/pathfind script (If there’s a bunch of moving parts)

Set all the parts of the NPC’s network owner to the first player that joins.

game.Players.PlayerAdded:Wait()

for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		v:SetNetworkOwner(workspace:WaitForChild(game.Players:GetPlayers()[1].Name))
	end
end

Would I run this on the server? RunService.Heartbeat doesn’t run really well on the server it can cause lag if used alot I think

Hey, thank you for solving this I was very confused by the other posts on the network ownership and all of that stuff.

1 Like