Having trouble with "attempt to index upvalue x ( a nil value)"

Okay so i’m pretty new to this (all i’ve done is the “Getting started” guides) and so I decided to take a stab at making my own pong-game using composer, This is the scene for the actual game( so far at least): 

-- Setting up physics local physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) local ball local usplatform local sovplatform function startmatch() -- Gets match going local whoStarts = math.random( 2 ) if ( whoStarts == 1 ) then ball:applyLinearImpulse( 0, 0.75, ball.x, ball.y ) elseif ( whoStarts == 2 ) then ball:applyLinearImpulse( 0, -0.75, ball.x, ball.y ) end end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- Getting Display Groups Up and Running (Background/Moving parts/UI) local backGroup = display.newGroup() sceneGroup:insert( backGroup ) local elementGroup = display.newGroup() sceneGroup:insert( elementGroup ) local uiGroup = display.newGroup() sceneGroup:insert( uiGroup ) -- Loading background local background = display.newImageRect( backGroup, "assets/background.png", 320, 620 ) background.x = display.contentCenterX background.y = display.contentCenterY local usplatform = display.newImageRect( elementGroup, "assets/usponger.png", 110, 50 ) usplatform.x = display.contentCenterX usplatform.y = display.contentCenterY+240 local sovplatform = display.newImageRect( elementGroup, "assets/sovponger.png", 110, 50 ) sovplatform.x = display.contentCenterX sovplatform.y = display.contentCenterY-240 local ball = display.newImageRect( elementGroup, "assets/ball.png", 20, 20 ) ball.x = display.contentCenterX ball.y = display.contentCenterY -- Actually making the game objects \*exist\* physics.addBody( usplatform, { friction=0.3, bounce=0.0 } ) physics.addBody( sovplatform, { friction=0.3, bounce=0.0 } ) physics.addBody( ball, { bounce=1.0 } ) physics.pause() end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen -- Getting match to start physics.start() startmatch() end end

But when I start the game I receive an error that reads:

“2pgame.lua:29: attempt to index upvalue ‘ball’ (a nil value)”

Note: On the menu scene i just display the images and added a button for this scene (i want to add another one for singleplayer later so figured i’d start from there)

Apologies if i’m missing something here, complete newbie to gamedev basically.

Thanks in advance.

Your problem resides here:

 local ball = display.newImageRect( elementGroup, "assets/ball.png", 20, 20 )

You are making “ball” local to the function scene:create(). The ball you declared local at the top of the scene is a different variable than the ball you’re declaring here.

The fix is simple, since you already have ball declared at the top, simply leave off the work “local” on this line so you will use the other variable.

Rob

A simple mistake, but other than that some nice clean code for someone new to it. :slight_smile:

Aaah I see. Thank you! 

Your problem resides here:

 local ball = display.newImageRect( elementGroup, "assets/ball.png", 20, 20 )

You are making “ball” local to the function scene:create(). The ball you declared local at the top of the scene is a different variable than the ball you’re declaring here.

The fix is simple, since you already have ball declared at the top, simply leave off the work “local” on this line so you will use the other variable.

Rob

A simple mistake, but other than that some nice clean code for someone new to it. :slight_smile:

Aaah I see. Thank you!