So I made a Follow Closet Player but the pathfinding doesn’t work here is the code
moveid = moveid + 1
local id = moveid
--//Tunnells PathFinder//
function findNearestTorso(pos)
local list = game.Workspace:children()
local dist = 1000
local temp = nil
local temp2 = nil
local human = nil
local torso = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait()
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target then
local path = game:GetService("PathfindingService"):FindPathAsync(script.Parent.HumanoidRootPart.Position, target.CFrame.p)
if path.Status == Enum.PathStatus.Success then
--//WayPoints//
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Block"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
end
for i = 6, #waypoints do
if id ~= moveid then break end
local waypoint = waypoints[i]
local destination = waypoint.Position
script.Parent.Humanoid:MoveTo(destination)
local connection;
if waypoint.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
connection = script.Parent.Humanoid.Changed:Connect(function()
script.Parent.Humanoid.Jump = true
end)
end
local reached = script.Parent.Humanoid.MoveToFinished
local function onPathBlocked(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex > waypoints then
-- Call function to re-compute the path
findNearestTorso(destination)
end
end
-- Connect 'Blocked' event to the 'onPathBlocked' function
path.Blocked:Connect(onPathBlocked)
if connection then
connection:Disconnect()
end
if not reached then
break
end
end
else
end
end
end
