Im having trouble making these npc following their own waypoints. The waypoints and the script work. But the thing is that whenever they get to waypoint1, they like go to waypoint2, and then go back to waypoint1, and not 3. This is the script:
local npc = script.Parent
local npcName = npc.Name
local waypointFolder = workspace:FindFirstChild("Waypoints_" .. npcName)
if not waypointFolder then
warn("Waypoints folder not found for " .. npcName)
return
end
local waypoints = waypointFolder:GetChildren()
table.sort(waypoints, function(a, b)
return a.Name < b.Name
end)
local currentIndex = 1
function Patrol()
if #waypoints == 0 then return end
local wp = waypoints[currentIndex]
if npc.Humanoid then
npc.Humanoid.AutoRotate = true
end
npc.Humanoid:MoveTo(wp.Position, wp)
local success = npc.Humanoid.MoveToFinished:Wait(5)
task.wait(0.00005)
currentIndex += 1
if currentIndex > #waypoints then
currentIndex = 1
end
end
while true do
Patrol()
task.wait()
end