Trying to use PathfindingServiec however running into constant issues with pathfinding not working as the developer hub says it would. It doesn’t take a direct route and it doesn’t take into account blocked paths
This is more or less copy pasted from here
local PathfindingService = game:GetService("PathfindingService")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local SetPath = PathfindingService:CreatePath()
task.wait(2)
local function followPath(destination)
print(destination)
-- Compute the SetPath
local success, errorMessage = pcall(function()
SetPath:ComputeAsync(workspace.Rig:GetPivot().Position, destination)
end)
if success and SetPath.Status == Enum.PathStatus.Success then
-- Get the SetPath waypoints
waypoints = SetPath:GetWaypoints()
-- Detect if SetPath becomes blocked
blockedConnection = SetPath.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the SetPath
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting SetPath blockage until SetPath is re-computed
blockedConnection:Disconnect()
-- Call function to re-compute new SetPath
followPath(destination)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = workspace.Rig.Humanoid.MoveToFinished:Connect(function(reached)
print(reached, nextWaypointIndex < #waypoints)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
workspace.Rig.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
workspace.Rig:PivotTo(workspace.Start.CFrame)
reachedConnection:Disconnect()
blockedConnection:Disconnect()
SetPath = PathfindingService:CreatePath()
waypoints = nil
nextWaypointIndex = nil
reachedConnection = nil
blockedConnection = nil
followPath(workspace.End.Position)
end
end)
end
-- Initially move to second waypoint (first waypoint is SetPath start; skip it)
nextWaypointIndex = 2
workspace.Rig.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
followPath(workspace.End.Position)
I’ve tried doing
local SetPath = PathfindingService:CreatePath({
Costs = {
Block = math.huge
}
})
with a modifier inside the black part and Label set to Block. But this still does nothing