So, I have this piece of code here.
local osana = game.Workspace.Osana
local osanaPathPoint = game.Workspace.OsanaEndingPoint
local PathfindingService = game:GetService("PathfindingService")
local humanoid = osana.Humanoid
local walkAnim = humanoid:LoadAnimation(script.Parent.walk)
local agentParams = {
AgentRadius = 1,
AgentHeight = 2,
AgentCanJump = false,
}
local path = PathfindingService:CreatePath(agentParams)
path:ComputeAsync(osana.HumanoidRootPart.Position, osanaPathPoint.Position)
local waypoints
local currentWaypointIndex
local function followPath(destinationObject)
-- Compute and check the path
path:ComputeAsync(osana.HumanoidRootPart.Position, destinationObject.Position)
-- Empty waypoints table after each new path computation
waypoints = {}
if path.Status == Enum.PathStatus.Success then
-- Get the path waypoints and start zombie walking
waypoints = path:GetWaypoints()
print(humanoid.DisplayName.."'s path is blocked but it has been recalculated. Is a player or another object in the NPC's way?")
-- Move to first waypoint
currentWaypointIndex = 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
-- Error (path not found); stop humanoid
humanoid:MoveTo(osana.HumanoidRootPart.Position)
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end
local function onPathBlocked(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex > currentWaypointIndex then
-- Call function to re-compute the path
followPath(osanaPathPoint)
end
end
path.Blocked:Connect(onPathBlocked)
humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(osanaPathPoint)
It's supposed to avoid any obstacle, but apparently it does detect blocked paths but doesn't detect NPCs and players. This is not good since the game I'm working on will have 82 NPCs pathfinding their way through only 3 narrow gates and they'll most likely get stuck. What is a possible solution?
