Attempt to compare nil with number

I have this code for a pong game I am making: 

local function removeBall() if (ball.x \< 0) or (ball.x \> 1200) then display.remove(ball) print("removed") end end Runtime:addEventListener("enterFrame", removeBall)

But I keep getting this error:

ERROR: Runtime error main.lua:28: attempt to compare nil with number stack traceback: main.lua:28: in function '?' ?: in function \<?:172\>

And it points to this line:

if (ball.x \< 0) or (ball.x \> 1200) then

This is how I made the ball:

local ball = display.newCircle(display.contentCenterX, display.contentCenterY, 20) physics.addBody(ball, "dynamic", {isSensor = true}) ball:setLinearVelocity(math.random(-400, 400), math.random(-200, 200))

Any help would be appreciated.

My first thought is that it’s a scoping error - did you access ball.x from another function?

When I have these issues, I use a few print statements.

e.g.: When you have created the ball, I use ‘print (ball)’ and i should see the table name.

Since you are using the function ‘removeBall’, is ball defined before that or inside another function? That would make it a scoping problem.

Try ‘print (ball)’  in different areas and you’ll see how it will flip from a table entry to nil (as it is out of scope)

Hopefully that makes sense.

Scoping is my guess as well.  I would forward declare the ball variable at the top of your scene, and then remove the ‘local’ when you actually make the ball.

Hello,

You forgot to cancel the Runtime listener after you have removed the ballon from the display.

removeBall is called eachFrame, and after the ballon is removed, the fonction is called again, and try to compare ballon.x, which is equal to nil, with the number 0. So the bug happens.

Just add " Runtime:removeEventListener(“enterFrame”,ballon) " in the if-condition

local function removeBall() if (ball.x \< 0) or (ball.x \> 1200) then Runtime:removeEventListener("enterFrame",ballon) // \<\< stop the listener here display.remove(ball) print("removed") end end Runtime:addEventListener("enterFrame", removeBall)

Thanks!

My first thought is that it’s a scoping error - did you access ball.x from another function?

When I have these issues, I use a few print statements.

e.g.: When you have created the ball, I use ‘print (ball)’ and i should see the table name.

Since you are using the function ‘removeBall’, is ball defined before that or inside another function? That would make it a scoping problem.

Try ‘print (ball)’  in different areas and you’ll see how it will flip from a table entry to nil (as it is out of scope)

Hopefully that makes sense.

Scoping is my guess as well.  I would forward declare the ball variable at the top of your scene, and then remove the ‘local’ when you actually make the ball.

Hello,

You forgot to cancel the Runtime listener after you have removed the ballon from the display.

removeBall is called eachFrame, and after the ballon is removed, the fonction is called again, and try to compare ballon.x, which is equal to nil, with the number 0. So the bug happens.

Just add " Runtime:removeEventListener(“enterFrame”,ballon) " in the if-condition

local function removeBall() if (ball.x \< 0) or (ball.x \> 1200) then Runtime:removeEventListener("enterFrame",ballon) // \<\< stop the listener here display.remove(ball) print("removed") end end Runtime:addEventListener("enterFrame", removeBall)

Thanks!