Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Continuous NPC pathfinding causes lag?

Asked by
WoTrox 345 Moderation Voter
3 years ago
Edited 3 years ago

I'm trying to make an NPC follow a player with pathfinding, but I'm kind of stuck now.

local function getPlayer(plr)
    local path = ps:CreatePath()
    path:ComputeAsync(thing.HumanoidRootPart.Position, plr.Character.HumanoidRootPart.Position); -- the "thing" is the npc

    local wp = path:GetWaypoints();
    thing.Humanoid:MoveTo(wp[2].Position);
    thing.Humanoid.MoveToFinished:Connect(function()
        wait();
        getPlayer(plr);
        return;
    end)
end

This is what I have made so far. I didn't use a for loop and go through all the waypoints and then create a new path, because if the player changes direction when the path is created, the NPC will still walk to the end of that path. So now it always moves towards the player, even if it changes its direction.

The problem now is that the NPC stops for a bit every time it reaches a waypoint, and also after like 5 seconds of moving, the whole server starts lagging (that's why I put the return there, but it doesn't seem to work). What am I doing wrong?

Answer this question