Table Functions

I am trying to have a Nuke Balloon remove all balloons on the screen. Currently whenever I run this function it returns nil for the balloons

local function RemoveAllBalloons()     for i=1, #balloons do         print(balloons[i].MyName)     end end  

Therefore whenever I change the print to balloons[i]:removeSelf() of course it is going to result in an error.

Whenever i spawn a balloon this is the code that I use

local function SpawnBalloon()         local balloon = display.newImageRect( "Balloon.png", 72, 72 )         balloon.y = display.contentHeight + 150         balloon.rotation = 0         balloon.MyName = "Balloon"         balloon.score = 1;         balloon.positioninballons = #balloons+1         balloon.x = math.random(30, display.contentWidth - 30)         group:insert( balloon )         table.insert( balloons, balloon.positioninballons, balloon )          physics.addBody( balloon, { density=1.0, friction=0.3, bounce=0.3 } )         local function touchHandler( event )             balloon:removeSelf()             table.remove(balloons, positioninballons)             myData.score = myData.score + 1             PlayerScore.text = "Score: " .. myData.score         end         balloon:addEventListener("touch", touchHandler) end  

Spawns the balloon no problem and everything works fine. Now when it comes to the nuke balloon the touchHandler function is a bit different

local function touchHandler( event )             balloon:removeSelf()             table.remove(balloons, positioninballons)             myData.score = myData.score + 1             PlayerScore.text = "Score: " .. myData.score             transition.to(flash, {alpha=1, time=50})             RemoveAllBalloons()             timer.performWithDelay(100, ResetFlash, 1)         end         balloon:addEventListener("touch", touchHandler)  

So I am pretty sure that I have everything written correctly, as in syntax that is. There is just a problem when it comes to removing them or even printing them. I have no idea why every single table index is coming back as nil and if anyone can explain to me why that would be much appreciated. 

do this:

local balloon = {} local function SpawnBalloon() local currBalloon = #balloon+1         balloon[currBalloon] = display.newImageRect( "Balloon.png", 72, 72 )         balloon[currBalloon].y = display.contentHeight + 150         balloon[currBalloon].rotation = 0         balloon[currBalloon].MyName = "Balloon"         balloon[currBalloon].score = 1;         balloon[currBalloon].positioninballons = [currBalloon]         balloon[currBalloon].x = math.random(30, display.contentWidth - 30)         group:insert( balloon[currBalloon] )          physics.addBody( balloon[currBalloon], { density=1.0, friction=0.3, bounce=0.3 } )         local function touchHandler( event )             balloon[currBalloon]:removeSelf()             balloon[currBalloon] = nil             myData.score = myData.score + 1             PlayerScore.text = "Score: " .. myData.score         end         balloon[currBalloon]:addEventListener("touch", touchHandler) end  

*have not tested this*

do this:

local balloon = {} local function SpawnBalloon() local currBalloon = #balloon+1         balloon[currBalloon] = display.newImageRect( "Balloon.png", 72, 72 )         balloon[currBalloon].y = display.contentHeight + 150         balloon[currBalloon].rotation = 0         balloon[currBalloon].MyName = "Balloon"         balloon[currBalloon].score = 1;         balloon[currBalloon].positioninballons = [currBalloon]         balloon[currBalloon].x = math.random(30, display.contentWidth - 30)         group:insert( balloon[currBalloon] )          physics.addBody( balloon[currBalloon], { density=1.0, friction=0.3, bounce=0.3 } )         local function touchHandler( event )             balloon[currBalloon]:removeSelf()             balloon[currBalloon] = nil             myData.score = myData.score + 1             PlayerScore.text = "Score: " .. myData.score         end         balloon[currBalloon]:addEventListener("touch", touchHandler) end  

*have not tested this*