So I’m trying to make good pathfinding where it goes for the closet player and if theres no close player it starts to wonder around the map, E.G like the rake
local moveid = 0
moveid = moveid + 1
local id = moveid
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)
function findTarget(pos)
local list = game.Workspace:children()
local dist = 150
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 wait() do
local target = findTarget(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
local waypoints = path:GetWaypoints()
for i = 2, #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:Wait()
if connection then
connection:Disconnect()
end
if not reached then
break
end
end
else
script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.CFrame * Vector3.new(0, 0, 2))
script.Parent.Humanoid.MoveToFinished:Wait()
end
end
end