I’m trying to make this NPC move smoothly when the path becomes blocked and the only viable path requires a jump.
I have two walls that spawn when the npc touches a pad on his way to goal which blocks him in and leaves the only way out as a 2 stud tall block the npc can jump over. Whenever the path recalculates over the jumpable wall, blocked fires every second and connects to a function that recalculates the path which makes the npc stall every second during computation. Here’s the script:
local wps = {}
local path = pfs:CreatePath()
local wpIdx = 1
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local treasure = workspace.Treasure
local function followPath(goal)
path:ComputeAsync(hrp.Position, goal.Position)
wps = {}
if path.Status == Enum.PathStatus.Success then
wps = path:GetWaypoints()
wpIdx = 1
hum:MoveTo(wps[wpIdx].Position)
else print ("No path available.")
end
end
hum.MoveToFinished:Connect(function(reached) --MoveToFinished fires when goal hit from MoveTo
if reached and wpIdx < #wps then
wpIdx += 1
if wps[wpIdx].Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(wps[wpIdx].Position)
end
end)
path.Blocked:Connect(function(blockedWPIdx)
if blockedWPIdx > wpIdx then
print("blocked")followPath(treasure)
end
end)
wait(6)
followPath(treasure)
Does anyone have any recommended edits to make the NPC find the path in a smoother fashion? I’m new to learning NPC AI abilities. Thanks for any ideas!