My NPC is stuttering whenever the path is computed and the NPC gets told to follow the path.
Here is my code, I’m pretty sure it has to do with the :MoveToFinished(), and to fix it use .magnitude but I’m not sure how to set it up properly. Any help?
local pathfindingService = game:GetService("PathfindingService")
local humanoid = workspace.NPC.Humanoid
local body = workspace.NPC:FindFirstChild("HumanoidRootPart")
local destination = goal.Position
local path = pathfindingService:CreatePath()
path:ComputeAsync(body.Position, destination)
local waypoints = path:GetWaypoints()
for k, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid.MoveToFinished:Wait()
end
Using an event to wait is much better than using eait(), because wait can be unreliable, especially for the purpose your using it for. Event waits can fire right when it’s finished when wait in slow servers can take much longer to complete.
The most likely reason it’s stuttering is because of its NetworkOwnership. Roblox splits the parts to all the clients to calculate physics instead of it doing all the work, and we can force the server or different clients to take ownership of it. If you set all the parts inside the characters network ownership to nil, that will give the server ownership of it. That should also remove the stuttering between waypoints.
Note, anytime it’s anchored or welded to an anchored part you need to reset its network ownership as that resets it.
I agree that MoveToFinished:Wait() is a far better way to handle things, but I too have also experienced the jerky behaviour of NPCs & Roblox Pathfinding.
I’ve just experimented with a variety of NPC scripts with some using MoveToFinished:Wait() and others a .magnitude check. Those that behave in the same way as the OP describes, adding script.Parent.PrimaryPart:SetNetworkOwner(nil) to the control script still results in the same jerky walking pattern . It’s not always consistent which is the frustrating part.
Lets see what the OP finds works best for them. I am not necessarily right and do not proclaim to be so. I just try to help with what little have learnt so far.
I have the NPC pathfinding function on a client script to help reduce lag even more, but I’m still experiencing stuttering. Should I still set the network owner to nil, or try out @BadDad2004’s method of .magnitude?
Stuttering isn’t the path creation itself its the :MoveTo() and :MoveToFinished()
I use :MoveToFinished:Wait() and have no issues. Check what is going on from :MoveToFinished and the next :MoveTo() it could be that something in there is slowing the script down.