Hello, Developers to I was messing around with PathfindingService and I occured a weird bug well it seems bug to me, I am trying to move npc with PathfindingService and it works really fine, but when I get near the npc its starts glitching you would understand it better when I will show you the video below.
Script
local sp = script.Parent;
local Humanoid = sp:WaitForChild("Humanoid");
local HRP = sp:WaitForChild("HumanoidRootPart");
local PathfindingService = game:GetService("PathfindingService");
local Workspace = game:GetService("Workspace");
local WalkTo = Workspace.Point;
local Path = PathfindingService:FindPathAsync(HRP.Position,WalkTo.Position);
local WalkTo = Path:GetWaypoints();
if (Path.Status == Enum.PathStatus.Success) then
for i,v in next,WalkTo do
Humanoid:MoveTo(v.Position);
Humanoid.MoveToFinished:Wait();
if (v.Action == Enum.PathWaypointAction.Jump) then
Humanoid.Jump = true;
end
end
end
I’m not really sure, but I believe it’s because the waypoints are positioned 4x4. Edit: You could check if the NPC is 4 studs or closer to the player, then move the NPC to the player.
The script is probably on the server, since the client can’t see the script, the client assumes that the NPC is not supposed to move which could be the reason why the NPC starts lagging when the player is near. In other words, the client and server is not agreeing on the NPC’s position.
In the meantime while the staff hasn’t said anything, try setting the NetworkOwnership of the parts of the NPC to the server by using Part:SetNetworkOwner() in a for loop. It wouldn’t hurt to try since you could always undo this.
Edit:
Doing this on a local script will cause replication issues.
You could just use the Part’s base class, BasePart, to check the class. This will catch all parts rather than a specific type of them. Also; there is a chance that parts are not immediate children of a character, such as accessories. In this case, you’ll want to use GetDescendants instead of GetChildren to catch everything.
for _, part in pairs(NPC:GetDescendants()) do
if part:IsA("BasePart") then
part:SetNetworkOwner(nil)
end
end
If you want to take that a step further, you can make a custom handler function that will set the network ownership of existing parts and any new descendants that get added.
Good to catch network ownership though. Not just for performance, but for security reasons as well. I remember not having a care in the world about network ownership and how it wasn’t a huge topic until it became apparent that it’s usage is important. Also prevents exploiters from tampering with NPCs if ownership is passed to them.