How Do I Pathfind multiple npcs from 1 script

So i have an issue where i want to tell multiple npcs to move with one script

they keep moving 1 after another and not together

My goal is to make all npcs move together into 1 position

function UnitControl:Move(pos: Vector3)
	
	for i,unit in pairs(ControlledUnits) do
		unit.Humanoid:MoveTo(pos)
		path:ComputeAsync(unit.PrimaryPart.Position,Vector3.new(pos.X,unit.PrimaryPart.Position.Y,pos.Z))
		if path.Status ~= Enum.PathStatus.NoPath then
			local points = path:GetWaypoints()
			if points and points[3] then
				print(3)
				unit.Humanoid:MoveTo(points[3].Position)
				
			end
		end
		
	end
	
end
2 Likes

You can use coroutines or spawn for it.

function UnitControl:Move(pos: Vector3)
    for i, unit in pairs(ControlledUnits) do
        coroutine.wrap(function()
            unit.Humanoid:MoveTo(pos)
            path:ComputeAsync(unit.PrimaryPart.Position, Vector3.new(pos.X, unit.PrimaryPart.Position.Y, pos.Z))
            if path.Status ~= Enum.PathStatus.NoPath then
                local points = path:GetWaypoints()
                if points and points[3] then
                    print(3)
                    unit.Humanoid:MoveTo(points[3].Position)
                end
            end
        end)()
    end
end
2 Likes

Perhaps try creating a new thread every time you are calling MoveTo, as it’s most likely yielding What @5smokin sent above me should work :stuck_out_tongue:

2 Likes