Resetting a group or removing all items from a group

At the end of a round of my game I am attempting to remove all items from a group using the following code

  
-- --------------  
-- removeAllBalls  
-- --------------  
  
local function removeAllBalls()  
  
 print ("removeAllBalls " .. allBallsGroup.numChildren)  
  
 for i = allBallsGroup.numChildren, 1, -1 do  
  
 print ("i = " .. i)  
  
 allBallsGroup:remove(i)   
  
 end  
  
end  
  

But I am getting the following error

Runtime error
…r/Documents/work/corona development/Frenzy5/main.lua:183: attempt to index global ‘allBallsGroup’ (a nil value)

Basically for each level of the game I display a different number of balls and I insert all the balls for the level into the group named “allBallsGroup”

This group is declared as local

  
local allBallsGroup = display.newGroup()  
  
allBallsGroup:insert( ball1 )  

Perhaps there is a better way to reset the group i.e remove all items from it before I populate it with the items for the next level. And out of interest should this remove code also remove the graphic from the screen?

Thanks

Paul
[import]uid: 7863 topic_id: 2743 reply_id: 302743[/import]

That sounds like a scope problem. If your group is declared below that function, place a predefining statement above the function.

[lua]local allBallsGroup[/lua] [import]uid: 5712 topic_id: 2743 reply_id: 8222[/import]

You hit the nail on the head - thanks Mike. I will get my head around this scoping soon.

[import]uid: 7863 topic_id: 2743 reply_id: 8226[/import]