I'm trying to make a pathfinding npc to find the nearest humanoid but it's not working. I can't figure out the part to make the npc find the nearest humanoid! Here's my script:
function findNearestHumanoid(position)
local allPals = game.Workspace:children()
local nearestHumanoid = nil
local dist = 10000
local humRoot = nil
local human = nil
local pal = nil
for x = 1, #allPals do --för varje #allPals. För första pal är x = 1. För andra loopen x=2 osv
pal = allPals[x]
if (pal.className == "Model") and (pal ~= script.Parent) then
humRoot = pal:findFirstChild("HumanoidRootPart")
human = pal:findFirstChild("Humanoid")
if (humRoot ~= nil) and (human ~= nil) and (human.Health > 0) then
if (humRoot.Position - position).magnitude < dist then
nearestHumanoid = humRoot.Parent
dist = (humRoot.Position - position).magnitude
end
end
end
end
return nearestHumanoid
end
local torso = script.Parent.Torso.Position
local human = script.Parent.Humanoid
local base = findNearestHumanoid()
local path = game:GetService("PathfindingService"):FindPathAsync(torso,base.Positon)
local points = path:GetWaypoints()
for i,v in pairs(points) do
local p = Instance.new("Part", workspace)
p.Anchored = true
p.CanCollide = false
p.CFrame = CFrame.new(v.Position)
p.Size = Vector3.new(1,1,1)
end
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(points) do
human:MoveTo(v.Position)
human.MoveToFinished:wait()
if v.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
end
end

4 comments
please fix the code blocks
thx
findNearestHumanoid needs an argument: position, but wasn't given one on line 26. i guess you wanted to pass in torso but forgot
Does not work.