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.
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
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.
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
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
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
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.
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