So the AI follows all the waypoint nodes but stops at the end, I tried doing a repeat loop but it won’t work. Any thoughts on this?
local function patrol()
local FindPath = workspace.Waypoints:GetChildren()
local Minimum = nil
local ClosestPath = nil
for i = 1, #FindPath do
local Humanoid = script.Parent:FindFirstChild("Humanoid")
local Body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UppperTorso")
if Body then
local Magnitude = (FindPath[i].Position - Body.Position).magnitude
if Minimum == nil or Magnitude < Minimum or Minimum < 10 then
Minimum = Magnitude
ClosestPath = FindPath[i]
end
end
end
if Minimum then
if Minimum < 500 then
return ClosestPath
end
end
end
while task.wait(0.2) do
local ClosestPath = patrol()
if ClosestPath then
-- get pathfinding service
local pathfindingService = game:GetService("PathfindingService")
-- Variables for NPC humanoid, torso, and destination
local Humanoid = script.Parent:FindFirstChild("Humanoid")
local Body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UppperTorso")
local Destination = ClosestPath.Position
-- create path object
local Path = pathfindingService:CreatePath()
-- compute a path
Path:ComputeAsync(Body.Position, Destination, math.huge)
-- get the waypoints table
local Waypoints = Path:GetWaypoints()
-- iterate through all waypoints, and jump when necessary
for k, Waypoint in pairs(Waypoints) do
Humanoid:MoveTo(Waypoint.Position)
-- change humanoid state to jump if necessary
if Waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid.MoveToFinished:Wait()
end
end
end