I was trying to use PathfindingService to see if I can control Player Movements instead of using WASD. I have run into an issue where, regardless of the positions, and models(Characters), it will always return “NoPath”. I can’t get it to just work.
ComputeAsync() will always return Enum.PathStatus.NoPath
ROBLOX’s DeveloperHub Example will return NoPath, but also errors on line 36 (maybe outdated?)
Using it on Player’s Character or NPC will both return NoPath
pathfinding-doesnt-work.rbxl (30.3 KB)
If you don’t want to download the place I was testing in, here are the two scripts I tried.
If someone can figure out why I am only getting a NoPath status, that’d be great.
Scripts
local PathfindingService = game:GetService("PathfindingService")
-- Variables for the zombie, its humanoid, and destination
local zombie = game.Workspace.Zombie
local humanoid = zombie.Humanoid
local destination = game.Workspace.PinkFlag
-- Create the path object
local path = PathfindingService:CreatePath()
-- Variables to store waypoints table and zombie's current waypoint
local waypoints
local currentWaypointIndex
local function followPath(destinationObject)
-- Compute and check the path
path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)
-- Empty waypoints table after each new path computation
waypoints = {}
print(tostring(path.Status))
if path.Status == Enum.PathStatus.Success then
-- Get the path waypoints and start zombie walking
waypoints = path:GetWaypoints()
-- Move to first waypoint
currentWaypointIndex = 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
-- Error (path not found); stop humanoid
humanoid:MoveTo(zombie.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(destination)
end
end
-- Connect 'Blocked' event to the 'onPathBlocked' function
path.Blocked:Connect(onPathBlocked)
-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("Event")
local PathfindingId
function ServerEvent(Player, ...)
local Args = {...}
local X,Y,Z = tonumber(Args[1]),tonumber(Args[2]),tonumber(Args[3])
if X and Y and Z then
local WorkId = tick()
PathfindingId = WorkId
print("Start of Pathfinding")
local Character = Player.Character
local Humanoid = Character.Humanoid
local Root = Character.HumanoidRootPart
local Position = Grid3(Vector3.new(X,Y,Z))+Vector3.new(0,2,0)
local Path = PathfindingService:CreatePath()
print("Computing Path from (["..typeof(Root.Position).."]"..tostring(Root.Position)..") to (["..typeof(Position).."]"..tostring(Position)..")")
Path:ComputeAsync(Root.Position, Position)
wait()
local Waypoints = Path:GetWaypoints()
print("["..#Waypoints.."] Waypoints")
if Path.Status ~= Enum.PathStatus.Success then warn("Failed to compute path '"..tostring(Path.Status).."'") return end
local Current = 0
local function NextWaypoint()
Current += 1
if Current > #Waypoints then return end
if PathfindingId ~= WorkId then return end
Humanoid:MoveTo(Waypoints[Current].Position)
end
Humanoid.MoveToFinished:Connect(NextWaypoint)
NextWaypoint()
end
end
function Grid3(Vector)
if typeof(Vector) ~= "Vector3" then return end
local X,Y,Z = (math.floor(Vector.X/4+0.5)*4),(math.floor(Vector.Y/4+0.5)*4),(math.floor(Vector.Z/4+0.5)*4)
return Vector3.new(X,Y,Z)
end
Event.OnServerEvent:Connect(ServerEvent)