Help: attempt to index global 'PBullet' (a nil value)

Hello people.
I have a problem with my coding. The Player Bullet should remove itself after it hits below y=0. Now everytime if that should happen, I get this error: attempt to index global ‘PBullet’ (a nil value)

Now, I don’t know how I can fix this? Any help. Here’s my code:

require "sprite"  
  
display.setStatusBar( display.HiddenStatusBar ) -- hide status bar  
system.setAccelerometerInterval(90)  
local background = display.newImage( "sand.png" )  
  
local Bullets = 0  
local Level = 20  
  
local PlayerSheet = sprite.newSpriteSheet("player.png", 32, 32)  
local PlayerMove = sprite.newSpriteSet(PlayerSheet, 1, 2)  
local Player = sprite.newSprite( PlayerMove )  
  
local EnemySheet = sprite.newSpriteSheet("enemy.png", 32, 32)  
local EnemyMove = sprite.newSpriteSet(EnemySheet, 1, 2)  
  
local WallSheet = sprite.newSpriteSheet("wall.png", 31, 480)  
local WallMove = sprite.newSpriteSet(WallSheet, 1, 31)  
sprite.add(WallMove, "Moving", 1, 31, 10)  
local WallLeft = sprite.newSprite(WallMove)  
local WallRight = sprite.newSprite(WallMove)  
  
WallLeft:prepare("Moving")  
WallRight:prepare("Moving")  
WallLeft:play()  
WallRight:play()  
  
WallLeft.x = 0  
WallLeft.y = display.contentHeight / 2  
WallRight.x = 320  
WallRight.y = display.contentHeight / 2  
  
Player.x = display.contentWidth / 2  
Player.y = 450  
Player.rotation = 270  
Player:play()  
  
local function onAccelerate( event )  
 Player.x = (Player.x + event.xGravity \* 12)  
 Player.y = (Player.y - event.yGravity \* 12)   
end  
  
local function onTap(event)  
 if "began" == event.phase then  
 if Bullets \< 3 then  
 local PBullet = display.newImage("pbullet.png")  
 PBullet.x = Player.x  
 PBullet.y = Player.y  
 PBullet.rotation = Player.rotation+90  
 Bullets = Bullets+1  
 transition.to(PBullet, {time=Level\*250, x = Player.x, y = -100})  
 end  
 end  
end  
  
local function spawnEnemy()  
 local Enemy = sprite.newSprite( EnemyMove )  
 physics.addBody( Enemy, "static", { friction=0, bounce=0 } )  
 Enemy:play()  
 Enemy.x = 28 + math.random ( 262 )  
 Enemy.y = -100  
 Enemy.rotation = 90  
 transition.to(Enemy, {time=math.random(10)+Level\*600, x = Enemy.x, y = 500})  
end  
  
-- Collision Detection  
  
-- Player Bullet  
if PBullet.x \< 0 then  
 PBullet:removeSelf()  
end  
  
timer.performWithDelay(2000, spawnEnemy, 0)  
  
timer.performWithDelay(10, moveBullet, 0)  
  
Runtime:addEventListener ("accelerometer", onAccelerate);  
Runtime:addEventListener ("touch", onTap);  

[import]uid: 8901 topic_id: 4045 reply_id: 304045[/import]

you’re only actually ever running this line once at startup

[lua]-- Player Bullet
if PBullet.x < 0 then
PBullet:removeSelf()
end[/lua]

and at that point you haven’t even created a PBullet, so yes it’s going to fail

rather than have a count of bullets, keep an array of bullets in use (then you can check the length of the array for your count) and check that array in a runtime enterframe listener

have a look at the Multi Puck example which shows you what you need to do

http://developer.anscamobile.com/content/multi-puck

[import]uid: 6645 topic_id: 4045 reply_id: 12383[/import]