title: How to pass a variable to lua script being executed inside scrapy from the command prompt?
body: <p>I am trying to pass a variable as user-define argument in scrapy which would be used in the for loop of lua script, my code is as follows:</p>
  <pre><code>import scrapy
  from scrapy_splash import SplashRequest
  from scrapy.selector import Selector
  
  
  class ProductsSpider(scrapy.Spider):
      name = 'allproducts'
  
      script = '''
          function main(splash, args)
             assert(splash:go(args.url))
             assert(splash:wait(0.5))
             result = {}
             local upto = tonumber(splash.number)
             for i=1,upto,1
             do
               #something
             end
             return output
          
          end
      '''
  
      def start_requests(self):
          url='https://medicalsupplies.co.uk'
          yield SplashRequest(url=url, callback=self.parse, endpoint='render.html', args={'wait':0.5})
          yield SplashRequest(url=url, callback=self.parse_other_pages, endpoint='execute',
              args={'wait':0.5, 'lua_source':self.script, 'number':int(self.number)}, dont_filter=True)
  
      def parse(self, response):
          for tr in response.xpath(&quot;//table[@id='date']/tbody/tr&quot;):
              yield{
                      'output' : #something
              }
  
      def parse_other_pages(self,response):
          for page in response.data:
              sel=Selector(text=page)
              for tr in sel.xpath(&quot;//table[@id='date']/tbody/tr&quot;):
                  yield{
                       'output' : #something
                     }
  </code></pre>
  <p>So, the issue I am facing is, when I run the for loop of the lua script using an interger i.e. <code>for i=1,5,1</code> the script works just fine but when I try to give an input to the script from the command prompt using <code>scrapy crawl allproducts -a number=5 -o test.json</code> while using   <code>for i=1,{self.number},1</code> for the for-loop inside the script, my code throws an error and I am not even able to use f-strings on this string, is there a way around on how to pass a variable to a text string(here called script) without breaking the code? I know I am not using the right syntax but I haven't found any resources for the same, appreciate any suggestions.</p>
  <p>The actual warning from the scraper is as follows:</p>
  <pre><code>WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string &quot;...&quot;]', 'line_number': 7, 'error': &quot;attempt to index global 'self' (a nil value)&quot;, 'type': 'LUA_ERROR', 'message': 'Lua error: [string &quot;...&quot;]:7: attempt to index global \'self\' (a nil value)'}}
  </code></pre>
  <p>Edit 1: from @Alexander's suggestions, modified the lua script and passed the variable as an integer argument to the SplashRequest, also instanciated the variable in the lua script using local (local upto = tonumber(splash.number))</p>
  <p>The warning right now is as follows:</p>
  <pre><code> WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string &quot;...&quot;]', 'line_number': 9, 'error': &quot;'for' limit must be a number&quot;, 'type': 'LUA_ERROR', 'message WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string &quot;...&quot;]', 'line_number': 9, 'error': &quot;'for' limit must be a number&quot;, 'type': 'LUA_ERROR', 'message': 'Lua error: [string &quot;...&quot;]:9: \'for\' limit must be a number'}}
  </code></pre>