Script makes only one model pathfind

i’m trying to make a script handle the pathfinding of every npc in a folder. there are 3 models in this folder. however, it only makes one model pathfind, which is odd.

local pathfinding_service = game:GetService("PathfindingService")
local collection_service = game:GetService("CollectionService")
local run_service = game:GetService("RunService")

local path
local waypoints
local personoid

local test_part = workspace:WaitForChild("test_part")
local tripmines = workspace:WaitForChild("tripmine_folder"):GetChildren()

path = pathfinding_service:CreatePath({
	AgentRadius = 2,
	AgentHeight = 1.2,

	Costs = {
		Water = 20,
		subspace = math.huge
	}
})

run_service.Heartbeat:Connect(function()
	
	for i, tripmine in ipairs(tripmines) do
		if tripmine:IsA("Model") then
			local hostile_part = tripmine.PrimaryPart
			if not hostile_part then 
				warn(`PrimaryPart not found for {tripmine}`)
			else	
				path:ComputeAsync(hostile_part.Position, test_part.Position)
				if path.Status == Enum.PathStatus.Success then
					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						local part = Instance.new("Part")
						part.Name = (`waypoint_{i}`)
						part.Anchored = true
						part.Size = Vector3.new(0.5, 0.5, 0.5)
						part.Material = Enum.Material.Neon
						part.Shape = Enum.PartType.Ball
						part.CanCollide = false
						part.Parent = workspace.Terrain
						part.Position = waypoint.Position
						
						game:GetService("Debris"):AddItem(part, 0.1)
						
						--[[
						if tripmine:FindFirstChildWhichIsA("Humanoid") then
							personoid = tripmine:FindFirstChildWhichIsA("Humanoid")
							personoid:MoveTo(waypoint.Position)
							personoid.MoveToFinished:Wait()
						end
						]]
					end
				end
			end
		end
	end
end)

my first instinct would be that the script is running before all of the children load in on the line
local tripmines = workspace:WaitForChild("tripmine_folder"):GetChildren()

if not that then maybe

  • experiment with using a different iterator for for i, tripmine in ipairs(tripmines) do (pairs?),
  • making sure the mines are infact Models,
  • or separating everything from line path:ComputeAsync(hostile_part.Position, test_part.Position) and onwards into a separate thread/routine since :ComputerAsync() yields
    (i don’t see how these would fix the problem but the roblox engine sometimes works in mysterious ways)

Another pointer. Please do not run that on Heartbeat. It’s completely unnecessary to update pathing 60/s. A while true loop would suffice with a