Error in main.lua

Can someone point me in the right direction… I’m getting “attempt to call method ‘removeSpriteWithUniqueName’ (a nil value)” in line 84 of my main.lua:

physics = require("physics")  
physics.start()  
  
display.setStatusBar( display.HiddenStatusBar )  
  
require "LevelHelperLoader"  
  
blueSky = display.newRect(0, 0, 320, 480)  
blueSky:setFillColor(0, 0, 0)  
score = display.newText("0", 30, 10, "Helvetica", 20)  
  
function startOver()  
 loadLevel()  
 Runtime:addEventListener("enterFrame", runtimeListener)  
 Runtime:addEventListener("accelerometer", accelerometerCall)  
 Runtime:addEventListener("touch", touchListener)  
  
end  
  
function gameOver()  
 gameOverText = display.newText("Game Over", 50, 240, native.systemFontBold, 40)  
  
 local function removeGOText()  
 gameOverText:removeSelf()  
 end  
  
 timer.performWithDelay(2000, removeGOText)  
  
 player:removePlayer()  
 Runtime:removeEventListener("enterFrame", runtimeListener)  
 Runtime:removeEventListener("accelerometer", accelerometerCall)  
 Runtime:removeEventListener("touch", touchListener)  
  
 loader:removeAllSprites()  
 localGroup = nil  
 timer.performWithDelay(2000, startOver)  
end  
  
local function newPlayer()  
  
 local p = loader:spriteWithUniqueName("player")  
  
  
 function pCollision(self, event)  
  
 object = event.other  
  
 if object.lhTag == LevelHelper\_TAG.CHECKPOINT then  
 print("CheckPoint")  
 gameWon()  
 end  
  
 if event.phase == "began" then  
 vx, vy = self:getLinearVelocity()  
 if vy \> 0 then   
 if object.lhTag == LevelHelper\_TAG.PLATFORM then  
 self:setLinearVelocity(0, -350)  
 audio.play(jump)  
 end  
  
 if object.lhTag == LevelHelper\_TAG.BCLOUD then  
 loader:removeSpriteWithUniqueName(object.uniqueName)  
 audio.play(explode)  
 end  
 end  
  
 if object.lhTag == LevelHelper\_TAG.MONSTER then  
 gameOver()   
 end  
 end  
 end  
 p.collision = pCollision  
 p:addEventListener("collision", p)  
  
  
  
  
 Runtime:addEventListener("enterFrame", p)  
  
 function p:removePlayer()  
 Runtime:removeEventListener("enterFrame", self)  
 loader:removeSpriteWithUniqueName(self.uniqueName)  
  
 end  
 return p  
end  
  
local function loadLevel()  
 localGroup = display.newGroup()  
 loader = LevelHelperLoader:initWithContentOfFile("level1.plhs")   
 loader:instantiateObjectsInGroup(physics, localGroup)  
   
 worldHeight = loader:getPhysicBoundariesRect().size.height  
 localGroup.y = -worldHeight + 480  
   
 player = newPlayer()  
end  
  
function touchListener(event)  
 if event.phase == "began" then   
 player:shootArrow(event.x, event.y)  
  
 local vx, vy = player:getLinearVelocity()  
 if event.x \> player.x then  
 player:setLinearVelocity(70, vy)  
 elseif event.x \< player.x then  
 player:setLinearVelocity(-70, vy)  
 end  
 end  
  
 if event.phase == "moved" then  
 local vx, vy = player:getLinearVelocity()  
 if event.x \> player.x then  
 player:setLinearVelocity(70, vy)  
 elseif event.x \< player.x then  
 player:setLinearVelocity(-70, vy)  
 end  
 end  
  
 if event.phase == "ended" then  
 local vx, vy = player:getLinearVelocity()  
 player:setLinearVelocity(0, vy)  
 end  
end  
function runtimeListener(e)  
 score.text = string.format('%d', worldHeight - 480 + localGroup.y)  
   
 if player.y \< -localGroup.y + 240 then   
 localGroup.y = -(player.y - 240)  
 elseif player.y \> -localGroup.y + 480 then  
 gameOver()  
 end   
   
  
   
  
end  
  
function gameWon()  
 print("you win")  
 timer.performWithDelay(2000, function() physics.pause() end)  
 gameWonText = display.newText("YOU WIN!", 50, 240, native.systemFontBold, 40)  
end  
  
function accelerometerCall(e)  
 px, py = player:getLinearVelocity()  
 player:setLinearVelocity(e.xGravity \* 700, py)  
end  
  
Runtime:addEventListener("accelerometer", accelerometerCall)  
Runtime:addEventListener("touch", touchListener)  
Runtime:addEventListener("enterFrame", runtimeListener)  
  
function angleBetween ( srcObj, dstObj )  
 local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y  
 local angleBetween = math.deg( math.atan( yDist/xDist ) )  
 if ( srcObj.x \< dstObj.x ) then angleBetween = angleBetween+90 else angleBetween = angleBetween-90 end  
 return angleBetween - 90  
end  
  
loadLevel()  

I discovered this because my gameOver function does not work, so I ran the debugger tool and it pointed me to the " loader:removeSpriteWithUniqueName(self.uniqueName)" as being an issue. Sorry about the crappy formatting. [import]uid: 125387 topic_id: 22411 reply_id: 322411[/import]

I’m no expert but

loader:removeSpriteWithUniqueName(self.uniqueName)

Shouldn’t it be

loader:removeSpriteWithUniqueName(self.player) [import]uid: 65840 topic_id: 22411 reply_id: 89343[/import]

Thanks for your reply, I initially thought the same thing but it made no difference :frowning:

Edit: I apologize, that DID work (I was looking at the wrong file)! Now of course I’m getting a new error, lol:
main.lua:72: attempt to index local ‘p’ (a nil value)
Runtime script error at file main.lua line 72

This is turning into quite a learning experience :slight_smile: [import]uid: 125387 topic_id: 22411 reply_id: 89346[/import]