Coming to the dev forum since i really need help.
Whats my issue?
My npcs walk jittery with their pathfinding script, which is a common problem when it comes to pathfinding and moveto()
Ive tried multiple things, adding debounces, waits, humanoid.MoveToFinished:Connect(function() then wrapping a wait in that. Ive tried distance based scripts to detect when they have reached the point and add a wait in. Ive tried everything, and nothing is working at all. When i do use the humanoid.MoveToFinished:Connect(function() it completely breaks my entire pathfinding script, the npcs just teleport to the camera parts and then don’t walk in the right way after.
I would really appreciate some help as ive been trying for ages and they still walk all jittery, the solutions i usually use won’t work and end up breaking other things in the script.
here is my script:
local PathfindingService = game:GetService("PathfindingService")
local npc = script.Parent.Parent
local humanoid = npc:WaitForChild("Humanoid")
local rootPart = npc:WaitForChild("HumanoidRootPart")
rootPart:SetNetworkOwner(nil)
local function moveThrough(positions)
for _, pos in ipairs(positions) do
local path = PathfindingService:CreatePath({
AgentRadius = 1,
AgentHeight = 3,
AgentCanJump = false,
AgentCanClimb = false
})
path:ComputeAsync(rootPart.Position, pos)
local waypoints = path.Status == Enum.PathStatus.Success
and path:GetWaypoints()
or { { Position = pos, Action = Enum.PathWaypointAction.Walk } }
for _, wp in ipairs(waypoints) do
humanoid:MoveTo(wp.Position)
humanoid.MoveToFinished:Wait()
wait()
end
end
end
local function getIndexedPositions(folderName, tagPrefix, count)
local folder = workspace.GameAssets.Waypaths:WaitForChild(folderName)
local points = {}
for i = 1, count do
local part = folder:FindFirstChild(tagPrefix .. i)
if part and part:IsA("BasePart") then
table.insert(points, part.Position)
end
end
return points
end
function startNPCMovement()
-- GameHall walk
moveThrough(getIndexedPositions("GameHall", "Waypoint", 5))
-- Random camera walk
local camId = math.random(1, 3)
local camTarget = workspace.GameAssets:FindFirstChild("CameraBoxNPC" .. camId)
if camTarget then
moveThrough({ camTarget:FindFirstChild("CamLine").Position })
-- Teleport sequence
rootPart.CFrame = CFrame.new(camTarget:FindFirstChild("TeleportStart").Position)
camTarget.MainPart.SMILE:Play()
task.wait(3)
rootPart.CFrame = CFrame.new(camTarget:FindFirstChild("TeleportEnd").Position)
camTarget.MainPart.CameraFlash:Play()
end
-- RLGL walk
moveThrough(getIndexedPositions("RLGL", "Diode", 17))
end
game.ReplicatedStorage.Remotes.AIGamehall.OnServerEvent:Connect(function()
wait(math.random(1,60))
startNPCMovement()
end)
if anyone can help then i greatly appreciate it.