I am creating a new pathfinding module and it works fine except for it only being compatible with rigs that don’t have a HumanoidRootPart. I don’t know why when I delete the rootpart it starts working but when there is a rootpart, the npc doesn’t follow the path. Any help is appreciated. Here is my script:
--// Services
local PathFindingService = game:GetService("PathfindingService")
--// OOP
local Path = {}
Path.__index = Path
--// Variables
local waypoints = nil
local AR, AH, ACJ = 5, 10, true
local currentWaypointIndex = 2
function onWaypointReached(reached, npc)
local humanoid = npc:FindFirstChildOfClass("Humanoid")
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end
function onPathBlocked(blockedWaypointIndex, npc, endpart, vector)
if blockedWaypointIndex > currentWaypointIndex then
if vector == true then
local path = Path.new(npc, vector)
path.Move()
else
local path = Path.new(npc, endpart)
path.Move()
end
end
end
function setPath(npc, endpart, vector)
local params = {AgentRadius = AR, AgentHeight = AH, AgentCanJump = ACJ}
local path = PathFindingService:CreatePath(params)
if npc:FindFirstChildOfClass("Humanoid")then
if vector ~= nil then
if npc:FindFirstChild("UpperTorso")then
path:ComputeAsync(npc:WaitForChild("UpperTorso").Position, vector)
path.Blocked:Connect(onPathBlocked, npc, endpart, vector)
elseif npc:FindFirstChild("Torso") then
path:ComputeAsync(npc:WaitForChild("Torso").Position, vector)
path.Blocked:Connect(onPathBlocked, npc, endpart, vector)
end
else
if npc:FindFirstChild("UpperTorso")then
path:ComputeAsync(npc:WaitForChild("UpperTorso").Position, endpart.Position)
path.Blocked:Connect(onPathBlocked, npc, endpart, vector)
npc.Humanoid.MoveToFinished:Connect(function(status)
onWaypointReached(status, npc)
end)
elseif npc:FindFirstChild("Torso")then
path:ComputeAsync(npc:WaitForChild("Torso").Position, endpart.Position)
path.Blocked:Connect(onPathBlocked, npc, endpart, vector)
npc.Humanoid.MoveToFinished:Connect(function(status)
onWaypointReached(status, npc)
end)
end
end
elseif npc:IsA("BasePart")then
if vector ~= nil then
path:ComputeAsync(npc.Position, vector)
else
path:ComputeAsync(npc.Position, endpart.Position)
end
end
return path
end
function Path.new(npc, endgoal, vector)
local newPath = {}
setmetatable(newPath, Path)
local getpath = setPath(npc, endgoal, vector)
newPath.FollowPath = getpath
newPath.Move = function()
if getpath then
waypoints = getpath:GetWaypoints()
local hum
waypoints = getpath:GetWaypoints()
if npc:FindFirstChildOfClass("Humanoid")then
hum = npc:FindFirstChildOfClass("Humanoid")
end
if getpath.Status == Enum.PathStatus.Success then
if hum then
if waypoints[currentWaypointIndex] then
local pos = waypoints[currentWaypointIndex].Position
hum:MoveTo(pos)
hum.MoveToFinished:Wait()
end
end
end
end
end
return newPath
end
return Path