timer.pause causing error

I am getting an error when I attempt to pause a timer and I would love to get help to fix it.

The error is:

?:0: attempt to index a nil value

stack traceback:

?: in function ‘pause’

here is my code: 

[lua]

physics = require(“physics”)

physics.start( )

physics.setDrawMode(“hybrid”)

physics.setGravity( 0, 0 )

local grpGame = display.newGroup()

local bg = display.newImage( grpGame, “assets/gfx/splash-bg.png”,W/2, H/2, W, H)

bg:scale( 1.1,1 )

local btnPause = display.newImage( grpGame, “assets/gfx/pause.png”,W*-.8/15,H*.5/10,W, H)

btnPause:scale(.3,.3)

btnPause.alpha = .5

local ballLeft = display.newImage( grpGame, “assets/gfx/blueball.png”, W*1/4, H/2, W, H)

ballLeft:scale( 1.5, 1.5 )

ballLeft.linearDamping = 0

local ballRight = display.newImage( grpGame, “assets/gfx/redball.png”, W*3/4, H/2, W, H)

ballRight:scale( 1.5, 1.5 )

ballLeft.linearDamping= 0

local playerPaddle = display.newImage( grpGame, “assets/gfx/playerpaddle.png”, W/2, H/2, W, H)

local enemyPaddleRight = display.newImage( grpGame, “assets/gfx/enemypaddleright.png”, W*10/10, H/2, W, H)

local enemyPaddleLeft = display.newImage( grpGame, “assets/gfx/enemypaddleleft.png”, W*0/10, H/2, W, H)

local wallLeft = display.newRect( grpGame, W*-1/10, H*5/10, 1, H*5 )

physics.addBody( wallLeft, “static”, {density=5.0, friction=.3, bounce=0} )

local wallRight = display.newRect( grpGame, W*11/10, H*5/10, 1, H*5 )

physics.addBody( wallRight, “static”, {density=5.0, friction=.3, bounce=0} )

local wallTop = display.newRect( grpGame, W*5/10, H*0/10, W*5, 1 )

physics.addBody( wallTop, “static”, {density=5.0, friction=.3, bounce=0} )

wallTop:scale( 1.2, 1)

local wallBottom = display.newRect( grpGame, W*5/10, H*10.01/10, W*5, 1 )

physics.addBody( wallBottom, “static”, {density=5.0, friction=.3, bounce=0} )

wallBottom:scale( 1.2, 1)

function pauseUntapped()

physics.start( )

timer.resume( scoreTimer )

end

local function pauseTapped()

physics.pause( )

timer.pause( scoreTimer )

scrnGame.isVisible = false

scrnPause.isVisible = true

end

function gameStart()

ballLeft.x = W*1/4

ballLeft.y = H/2

ballRight.x = W*3/4

ballRight.y = H/2

playerPaddle.x = W/2

playerPaddle.y = H/2

playerPaddle.rotation = 0

enemyPaddleLeft.x = W*10/10

enemyPaddleLeft.y = H/2

enemyPaddleLeft.rotation = 0

enemyPaddleRight.x = W*0/10

enemyPaddleRight.y = H/2

enemyPaddleRight.rotation = 0

local collisionFilter = { groupIndex = -2 }

physics.addBody( ballLeft, “dynamic”, {density=1.0, friction=.3, bounce=1, radius=6, filter=collisionFilter} )

physics.addBody( ballRight, “dynamic”, {density=1.0, friction=.3, bounce=1, radius=6, filter=collisionFilter} )

physics.addBody( playerPaddle, “kinematic”, {density=1.0, friction=0, bounce=1} )

physics.addBody( enemyPaddleLeft, “kinematic”, {density=1.0, friction=0, bounce=1} )

physics.addBody( enemyPaddleRight, “kinematic”, {density=1.0, friction=0, bounce=1} )

physics.start( )

timerStart()

ballLeft:setLinearVelocity( -50, 0)

ballRight:setLinearVelocity( 50, 010)

btnPause:addEventListener( “tap”, pauseTapped )

end

function timerStart()

–Score–

score = 0

scoreTxt = display.newText( grpGame,“Score: 0”, W/2, H*1/8, “Helvetica”, 18 )

function updateScore()

    score = score + 1

    scoreTxt.text = string.format(“Score: %d”, score)

end

scoreTimer = timer.performWithDelay(100, updateScore, 0)

–Score–

end

function gameQuit()

btnPause:removeEventListener( “tap”, pauseTapped )

physics.removeBody( ballLeft )

physics.removeBody( ballRight )

physics.removeBody( playerPaddle )

physics.removeBody( enemyPaddleLeft )

physics.removeBody( enemyPaddleRight )

scoreTxt:removeSelf( )

end

function onRedraw()

local ballLeftCoord = ballLeft.y

local ballRightCoord = ballRight.y

enemyPaddleRight.y = ballRightCoord

enemyPaddleLeft.y = ballLeftCoord

if ballLeft.x < enemyPaddleLeft.x or ballLeft.x > enemyPaddleRight.x 

or ballRight.x > enemyPaddleRight.x or ballRight.x < enemyPaddleLeft.x then

   – gameOver()

   print(“Hi”)

end

end

function gameOver()

scrnGameOver.isVisible = true

scrnGame.isVisible = false

end

function timerStart()

–Score–

local score = 0

local scoreTxt = display.newText( grpGame,“Score: 0”, W/2, H*1/8, “Helvetica”, 18 )

local function updateScore()

    score = score + 1

    scoreTxt.text = string.format(“Score: %d”, score)

end

local scoreTimer = timer.performWithDelay(100, updateScore, 0)

–Score–

end

local function movePlayerPaddle(event)

if event.phase == “began” then

        markY = playerPaddle.y    – store y location of object

    elseif event.phase == “moved” then

        local y = (event.y - event.yStart) + markY

        

        playerPaddle.y = y    – move object based on calculations above

    end

    

    return true

end

local totaltime = 0

playerPaddle:addEventListener( “touch”, movePlayerPaddle )

Runtime:addEventListener(“enterFrame”, onRedraw)

grpGame.isVisible = false

return grpGame

[/lua]

any help would be appreciated.

Hi @bmitr334,

This may be a scoping issue. Try declaring a local upvalue (reference) of “scoreTimer” near the top, then use that as your primary timer, and see if the “nil value” errors stop.

Best regards,

Brent

Hi @bmitr334,

This may be a scoping issue. Try declaring a local upvalue (reference) of “scoreTimer” near the top, then use that as your primary timer, and see if the “nil value” errors stop.

Best regards,

Brent