Pathfinding NPC Stutters for no apperant reason, after working just fine

Hello fellow Developers!

Im making a Sequel to one of my older games called Retail Store (goofy puzzle, destroy store and annoy npcs game), and ran into a issue with my new NPCs.

My issue is that after about 5 repeats, they start stuttering seemingly for no reason.
I have tried looking something up but found nothing.
https://gyazo.com/50c91d5801c47f2d16b128e7d695590c

Everything works fine, one NPC in the video even walks smoothly, but after a while starts stuttering like the others. They even go back to being smooth aswell.

Server Script located inside the NPC:

script.Parent.PrimaryPart:SetNetworkOwner(nil)

while true do
	local RandomPosition = workspace.Map.Scripts.CustomerPositionParts:GetChildren()[math.random(1,13)]
	local Path = game:GetService("PathfindingService"):CreatePath()
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, RandomPosition.Position)
	for i,v in ipairs(Path:GetWaypoints()) do
		script.Parent.Humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	task.wait(Random.new():NextNumber(1,3))
end

https://gyazo.com/caa0b6d101d348ec19929eb486742a7d

Ignore the bland looking game, im focusing on the Scripting first.
Thanks for reading this and any help is appriciated!

5 Likes

The stutters I have fixed come from the model having multiple paths at the same time and trying to complete both. Double check that there is only one copy of the script per NPC.

1 Like

Is the script fine otherwise?
Here is a picture of the NPC
https://gyazo.com/c5ad7a4c601e542a6770a2c09ce8c972
“AI” is the script above.

The way you have set up your loop, I can’t break it. one thing you have done that I do NOT like is you define a new variable Path inside the loop, for every pass. I would use only one variable.

Try this to see if life improves:

script.Parent.PrimaryPart:SetNetworkOwner(nil)
local Path, RandomPosition = nil, nil

while true do
	RandomPosition = workspace.Map.Scripts.CustomerPositionParts:GetChildren()[math.random(1,13)]
	Path = game:GetService("PathfindingService"):CreatePath()
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, RandomPosition.Position)
	for i,v in ipairs(Path:GetWaypoints()) do
		script.Parent.Humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
    Path = nil
	task.wait(Random.new():NextNumber(1,3))
end
1 Like

I’ll let it run for a bit, thank you for your time so far!^^

Same issue applies sadly. Stuttering continues

1 Like

Here is a Simple script
it should work without that stutters

Edit:
i am running the script for about 10 Minutes now and it’s working fine for me

local Move = true

local function moveTo(humanoid, targetPoint)
	local targetReached = false

	Move = false
	
	local connection
	
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
		Move = true
	end)

	humanoid:MoveTo(targetPoint)

	while not targetReached do

		if not (humanoid and humanoid.Parent) then
			break
		end

		if humanoid.WalkToPoint ~= targetPoint then
			break
		end

		humanoid:MoveTo(targetPoint)

		humanoid.MoveToFinished:Wait()

	end

	if connection then
		connection:Disconnect()
		connection = nil
		Move = true
	end
end

task.wait(3) --// wait for 3 sec Just For Test

print("Move")

script.Parent.PrimaryPart:SetNetworkOwner(nil)

while Move do

	local RandomPart = workspace.Path:GetChildren()

	local RandomPosition = math.random(1,#RandomPart)

	local Path = game:GetService("PathfindingService"):CreatePath()

	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, RandomPart[RandomPosition].Position)

	for i,v in ipairs(Path:GetWaypoints()) do
		moveTo(script.Parent.Humanoid,v.Position)
	end
	task.wait(Random.new():NextNumber(1,3))
	
end

Its still happening and they seem to have lost their brain aswell (they get stuck alot now / stop walking halfway through)
https://gyazo.com/9b4fd732af4d0a2470d19dbb9fe639ba
https://gyazo.com/dcea2487f85ee926cb438e70041419a0
https://gyazo.com/51cd2dd94222cb60fc185a3d93d5d8c1

Edit:
It like slowly builds up, works fine, stutter begins, stop walking halfway through.

1 Like

Maybe there is another script is Moving them!
for me this script is working fine (as i said, i ran it for over 15Mins and nothing from this happen to them)

and Question dose the NPCs has the same CollisionGroupId With Models like this or not?
if they don’t have the same CollisionGroupId try to make them Grouped CollisionGroup

image

Theres nothing other than your script moving the npc.
It like slowly builds up, works fine, stutter begins, stop walking halfway through.

NPC CollisiongroupId is 1 and General Models 0

1 Like

if you want better Movement for the npc try to make a Group Collision with the Models

image

Wouldnt they walk through the objects?

Here the game so you can see the issue for yourself.

1 Like

they will do but better then stuck in them

image

i will try to fix that

i don’t know, But its working fine with me, its running for over 20Mins and nothing from this Happen to them
here is a Video :

do you want to look into the game in studio?
Edit: if yes i need to friend you in order to give edit permission.
Edit 2: You got perms^^

1 Like

There’s a few issues with this script. Part of the problem is that you are creating a new path variable every time the loop executes. You only need to do that once. But the big issue is that you are recomputing the path every time the loop executes, which is generally not needed. And you don’t need to recreate a random object each time either.

Take a look at my answer here: Pathfinding script not working properly (Solved) - #27 by Maelstorm_1973

Ended up fixing the issue myself! Thanks to finding a post with the exact same issue as mine.

I had to set ALL the parts networkowner to nil (server), not just the primarypart.

Final script for anyone who has this issue:

local Path = game:GetService("PathfindingService"):CreatePath()
local RandomPart = workspace.Map.NPCs.CustomerPositionParts:GetChildren()

for i,v in pairs(script.Parent:GetDescendants()) do
	if v:IsA("Part") or v:IsA("MeshPart") then
		v:SetNetworkOwner(nil)
	end
end

while true do
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, workspace.Map.NPCs.CustomerPositionParts:GetChildren()[math.random(1, #RandomPart)].Position)
	for i,v in ipairs(Path:GetWaypoints()) do
		script.Parent.Humanoid:MoveTo(v.Position)
		if v.Action == Enum.PathWaypointAction.Jump then
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	task.wait(Random.new():NextNumber(1,3))
end

Thanks to anyone who read this / replied. <3

4 Likes