Hiya everyone,
I have recently started developing an NPC system to hone my scripting skills and to experiment with PathfindingService however this bug has been occuring for a few days now and I was not able to find a proper solution.
The problem is, after NPCs reach some random nodes I have given, then start their way to the next node, they do walk there however they start to stutter, doing one step at a time while it was smooth in the beginning
I was thinking it might be about waypoints however I could not find a proper solution.
Any help will be appreciated.
local GuestMover = {}
local PathfindingService = game:GetService("PathfindingService")
function GuestMover:MoveGuest(guest: Instance)
local Nodes = workspace:WaitForChild("Nodes")
if not Nodes then
warn("No nodes found to move NPCs")
return
end
-- Retrieve the humanoid and a random target node
local Humanoid = guest:WaitForChild("Humanoid")
local RandomNode = Nodes:GetChildren()[math.random(1, #Nodes:GetChildren())]
-- Create a path object
local Path = PathfindingService:CreatePath({
AgentRadius = 2, -- Adjust according to your NPC's size
AgentHeight = 5, -- Adjust according to your NPC's height
AgentCanJump = true,
AgentJumpHeight = 7.2, -- Customize for jump obstacles
AgentMaxSlope = 45 -- Adjust for sloped terrains
})
-- Compute the path to the random node
Path:ComputeAsync(guest:WaitForChild("HumanoidRootPart").Position, RandomNode.Position)
if Path.Status == Enum.PathStatus.Success then
local Waypoints = Path:GetWaypoints()
for _, Waypoint in ipairs(Waypoints) do
Humanoid:MoveTo(Waypoint.Position)
-- If the waypoint requires jumping, trigger jump
if Waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
-- Wait until the NPC reaches the current waypoint
local success = Humanoid.MoveToFinished:Wait()
if not success then
warn("Failed to reach waypoint")
break
end
end
else
warn("Pathfinding failed")
end
end
return GuestMover