Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

NPC Pathfinding Stuttering when a Player is nearby?

Hello!

I am currently making a game with an NPC that wanders around the map, and this works, smoothly, however, when the player gets near the NPC, the npc starts to stutter when walking. What I mean by stuttering is that instead of walking normally how it should it keeps on lagging and goes very slowly, and sometimes it even breaks the path it's trying to go to.

I am not sure what is the cause of this issue.

Here is the script I am using, it used 10 different locations for the NPC to go to:

local PathfindingService = game:GetService("PathfindingService")
local Nightmare = script.Parent

local Humanoid = Nightmare:WaitForChild("Humanoid")
local HumanoidRootPart = Nightmare:FindFirstChild("HumanoidRootPart")

--##[POSSIBLE POSITIONS]##--

local Pos1 = Vector3.new(-77.34, -11.133, 7.23)
local Pos2 = Vector3.new(-9.93, 4.777, 7.23)
local Pos3 = Vector3.new(43.15, -9.373, 19.86)
local Pos4 = Vector3.new(13.49, -6.183, 39.12)
local Pos5 = Vector3.new(-2.97, -6.183, 77.37)
local Pos6 = Vector3.new(-61.12, -6.183, 46.7)
local Pos7 = Vector3.new(-31.46, -6.183, -1.03)
local Pos8 = Vector3.new(-24.45, 8.907, -20.1)
local Pos9 = Vector3.new(4.3, 8.907, -24.05)
local Pos10 = Vector3.new(3.71, 8.907, -4.79)

--##[MOVE]##--

while true do
local Randomizer = math.random(1, 10)

	local Path = PathfindingService:CreatePath()
		if Randomizer == 1 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos1)
	end
	
		if Randomizer == 2 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos2)
	end
	
		if Randomizer == 3 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos3)
	end

	if Randomizer == 4 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos4)
	end

	if Randomizer == 5 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos5)
	end

	if Randomizer == 6 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos6)
	end

	if Randomizer == 7 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos7)
	end

	if Randomizer == 8 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos8)
	end

	if Randomizer == 9 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos9)
	end

	if Randomizer == 10 then
		Path:ComputeAsync(HumanoidRootPart.Position, Pos10)
	end
	
	
	if Path.Status == Enum.PathStatus.Success then
		local Waypoints = Path:GetWaypoints()
		for _, 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
	end
	wait(5)
end

If I could get some help with this it would be great. Thanks!

1 comment

0

I'd recommend using a table so you don't have 38 lines of if statements

1 answer

✓ Accepted answer

Well, it looks like you are not setting the NetworkOwner of the NPC to the server. The problem is when you are far from the NPC, NetworkOwner of your NPC was the server, but when you move close to it, the NetworkOwner changes, and it creates stutter and lag to the NPC.

To fix it, you can simple add a line :

HumanoidRootPart:SetNetworkOwner(nil) -- 'nil' equals server

And your code might look like this :

local PathfindingService = game:GetService("PathfindingService")
local Nightmare = script.Parent

local Humanoid = Nightmare:WaitForChild("Humanoid")
local HumanoidRootPart = Nightmare:FindFirstChild("HumanoidRootPart")

--##[POSSIBLE POSITIONS]##--

local Pos1 = Vector3.new(-77.34, -11.133, 7.23)
local Pos2 = Vector3.new(-9.93, 4.777, 7.23)
local Pos3 = Vector3.new(43.15, -9.373, 19.86)
local Pos4 = Vector3.new(13.49, -6.183, 39.12)
local Pos5 = Vector3.new(-2.97, -6.183, 77.37)
local Pos6 = Vector3.new(-61.12, -6.183, 46.7)
local Pos7 = Vector3.new(-31.46, -6.183, -1.03)
local Pos8 = Vector3.new(-24.45, 8.907, -20.1)
local Pos9 = Vector3.new(4.3, 8.907, -24.05)
local Pos10 = Vector3.new(3.71, 8.907, -4.79)

--##[MOVE]##--

while true do
HumanoidRootPart:SetNetworkOwner(nil)
local Randomizer = math.random(1, 10)

    local Path = PathfindingService:CreatePath()
        if Randomizer == 1 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos1)
    end

        if Randomizer == 2 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos2)
    end

        if Randomizer == 3 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos3)
    end

    if Randomizer == 4 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos4)
    end

    if Randomizer == 5 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos5)
    end

    if Randomizer == 6 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos6)
    end

    if Randomizer == 7 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos7)
    end

    if Randomizer == 8 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos8)
    end

    if Randomizer == 9 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos9)
    end

    if Randomizer == 10 then
        Path:ComputeAsync(HumanoidRootPart.Position, Pos10)
    end


    if Path.Status == Enum.PathStatus.Success then
        local Waypoints = Path:GetWaypoints()
        for _, 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
    end
    wait(5)
end

Lemme know if it helps!

1 comment

0

That worked! Thanks alot!

This discussion is preserved as it was. New answers, comments, and votes are closed.