Please ask any questions if you need more explaination!
Code:
local rootPart = script.Parent.HumanoidRootPart
local human = script.Parent.Humanoid
function findTarget()
local dist = 200
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local humanoid = v:FindFirstChild("Humanoid")
local head = v:FindFirstChild("Head")
if humanoid and head and v.Name ~= "slime" then
if (head.Position - rootPart.Position).magnitude < dist and humanoid.Health > 0 then
dist = (head.Position - rootPart.Position).magnitude
target = head
end
end
end
return target
end
while wait(1) do
local head = findTarget()
if head then
local ray = Ray.new(rootPart.Position, (head.Position - rootPart.Position).Unit * 200)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, (script.Parent))
if hit then
if hit:IsDecendantOf(head.Parent) then
human:MoveTo(head.Position)
else
human:MoveTo(head.Position)
wait(0.4)
path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head.Position)
points = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(points) do
human:MoveTo(v:Position)
human.MoveToFinished:wait(2)
if v.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
if (points[#points].Position - head.Position).magnitude > 15 then
break
end
end
else
human:MoveTo(head.Position)
end
end
end
end
end
Please correct any issues by copying the code and pasting the code corrected, I can’t understand otherwise too well

