So I made an npc pathfinding script for a horror game and it works fine until it starts to pathfinding, when it pathfinds it goes to a waypoint, stops for a tiny bit, and walks to the next. Any way to fix this?
local agentParams = {
["AgentHeight"] = 5,
["AgentRadius"] = 2,
["AgentCanJump"] = true,
Costs = {
Doors = 1
}
}
local path = pathfindingService:CreatePath(agentParams)
path:ComputeAsync(humanoidRootPart.Position, target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoint.Position)
if checkSight(target) then
repeat
wait()
humanoid:MoveTo(target.Position)
if target == nil or target.Parent.Humanoid == nil or target.Parent.Humanoid.Health < 1 or humanoid.Health < 1 then
break
end
until not checkSight(target)
break
end
humanoid.MoveToFinished:Wait()
end
else
repeat
wait()
local xRand = math.random(-5, 5)
local zRand = math.random(-5, 5)
local goal = humanoidRootPart.Position + Vector3.new(xRand, 0 ,zRand)
humanoid:MoveTo(goal)
humanoid.MoveToFinished:Wait()
break
until path.Status == Enum.PathStatus.Success
end
Waits are actually SUPER important and can be the difference between a 0.015-second delay to ensure the system doesn’t encounter an exhaust, and the system failing because of an exhaust. It is actually more so encouraged to use them, and they ONLY wait the said amount of time, no more or less. However, in the instance of this script, it would be better to use task.delay.
I recommend using task.delay(humanoid:MoveTo(), 0.01) in this instance, instead of task.wait! (task.wait is always good, however, task.delay does the same thing, just you can control it better!)
Not to disappoint you, but I haven’t figured this out as well. I would instead use something else than the pathfinding service ROBLOX gives you, as its really bad. You can either raycast the npc to its target or use a custom pathfinding module to help you. (Legacy) SimplePath - Pathfinding Module - #33 by V3N0M_Z
Sorry for not replying. I actually had a solution then but my computer just started glitching real bad
local path = pathfindingService:CreatePath(agentParams)
path:ComputeAsync(humanoidRootPart.Position, target.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
waypoints = path:GetWaypoints()
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end