title: Does a for loop iterate over an empty table in Lua and Love2d?
body: <p>I'm not quite sure on what's happening with the code right now. It's all written in Lua in VScode so I've been using only Alt+L to run it with the love extension, as I don't actually have a Lua compiler set up. When I run the code, the idea is it that I will click the screen and a bullet will travel in that direction, then after 0.5s it will disappear.</p>
  <p>However, what's happening is that after I spawn a bullet, it will exist for a time (I think 0.5s but I'm not quite sure) and then remove itself. That's all what I want, but then the calculations I did to find the direction the bullet should travel in and apply it to it's x and y values continues to occur even though the bullet is removed from the table. I'm not sure on this terminology and I've only been using LOVE for a day or two, so I don't quite know what's going on.</p>
  <p>Here's the code:</p>
  <pre><code>function love.load()
      window = {}
      window.x, window.y = love.graphics.getDimensions()
  
      player = {}
      player.speed = 5
      player.x = window.x/2
      player.y = window.y/2
      player.r = 15
      player.w = {15, 0.5} --speed, duration
  
      bullets = {} --x, y, direction, speed, duration
      direction = 0
  end
  
  function love.update(dt)
      for i=1, #bullets do
          bullets[i][1] = bullets[i][1] + bullets[i][4]*math.cos(bullets[i][3])
          bullets[i][2] = bullets[i][2] + bullets[i][4]*math.sin(bullets[i][3])
          bullets[i][5] = bullets[i][5] - dt
          if bullets[i][5] &lt;= 0 then
              table.remove(bullets, i)
          end
      end
  end
  
  function love.draw()
      love.graphics.circle('fill', player.x, player.y, player.r)
      love.graphics.print(direction)
      love.graphics.print('('..love.mouse.getX()..','..love.mouse.getY()..')',0,50)
      love.graphics.print('('..player.x..','..player.y..')',0,100)
      for i=1, #bullets do
          love.graphics.circle('fill', bullets[i][1], bullets[i][2], 5)
      end
  end
  
  function love.mousepressed(x, y, button, istouch, presses)
      if button == 1 then
          direction = math.atan((y-player.y)/(x-player.x))
          if player.x &gt; x then direction = direction + math.pi end
          direction = direction + math.random(-10, 10) * math.pi/180
          table.insert(bullets, {player.x, player.y, direction, player.w[1],player.w[2]})
      end
  end
  </code></pre>
  <p>When I run it and do what I said before, this is the error I recieve:</p>
  <pre><code>Error
  
  main.lua:18: attempt to index a nil value
  
  
  Traceback
  
  main.lua:18: in function 'update'
  [C]: in function 'xpcall'
  </code></pre>
  <p>Line 18 is this: <code>bullets[i][1] = bullets[i][1] + bullets[i][4]*math.cos(bullets[i][3])</code></p>
  <p>I have never really developed in Lua and this is my first time getting into game dev so I'm just experimenting, hence the probably very poorly written code. I appreciate any help, thanks!</p>