Remove Objects in groups help?

I need help this problem. Ok the problem is here when I recieve it in terminal

Runtime error
/Users/sebitttas/Desktop/Enix/gameselect.lua:269: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
/Users/sebitttas/Desktop/Enix/gameselect.lua:269: in function ‘cleanGroups’
/Users/sebitttas/Desktop/Enix/gameselect.lua:32: in function ‘onEvent’
/Users/sebitttas/Desktop/Enix/ui.lua:88: in function
?: in function <?:215>

(This terminal above matches with the code below)

Ok what this problem when I exit the level select and enter agiain the levels are not there anymore and when I go back to the menu screen it crashes.
here is the code for it

[code]module(…, package.seeall)

require “ui”
require “movieclip”

local ui = require(“ui”)
local slideView = require(“slideView”)

function new()
local localGroup = display.newGroup()
local slideGroup = display.newGroup()

backgroundMusic = audio.loadStream(“My Song.m4a”)

backgroundMusicChannel = audio.play( backgroundMusic, { channel=20, loops=-1, fadein=10000 } ) – play the background music on channel 1, loop infinitely, and fadein over 5 seconds
– play the laser on any available channel

local bk = display.newImage(“bk.png”)
bk.x = 240
bk.y = 160
localGroup:insert(bk)

local text = display.newText(“Level Select”, 170, 10,“Times New Roman”, 30)
localGroup:insert(text)

local function onback (event)
if event.phase == “release” then
director:changeScene (“menu”, “moveFromLeft”)
cleanGroups (slideGroup)
end
end

back = ui.newButton{
default = “arrow1.png”,
over = “arrow3.png”,
x = 30,
y = 30,
onEvent = onback,
}

localGroup:insert(back)

local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
if currentLevel == “empty” then
currentLevel = 1
saveFile(“currentLevel.txt”, currentLevel)
end
end
checkForFile()

–Table for levels
local level = {}

–Table for level text
local levelText = {}

–Function for open levels
local function goLevel (event)

director:changeScene(event.target.scene)
audio.stop( backgroundMusicChannel )
cleanGroups (slideGroup)
end

local startX = 45

iconPos = {}
iconPos[1] = {x=70, y=80}
iconPos[2] = {x=140, y=80}
iconPos[3] = {x=210, y=80}
iconPos[4] = {x=280, y=80}
iconPos[5] = {x=350, y=80}
iconPos[6] = {x=420, y=80}
iconPos[7] = {x=70, y=140}
iconPos[8] = {x=140, y=140}
iconPos[9] = {x=210, y=140}
iconPos[10] = {x=280, y=140}
iconPos[11] = {x=350, y=140}
iconPos[12] = {x=420, y=140}
iconPos[13] = {x=70, y=200}
iconPos[14] = {x=140, y=200}
iconPos[15] = {x=210, y=200}
iconPos[16] = {x=280, y=200}
iconPos[17] = {x=350, y=200}
iconPos[18] = {x=420, y=200}
iconPos[19] = {x=70, y=260}
iconPos[20] = {x=140, y=260}
iconPos[21] = {x=210, y=260}
iconPos[22] = {x=280, y=260}
iconPos[23] = {x=350, y=260}
iconPos[24] = {x=420, y=260}
iconPos[25] = {x=540, y=80}
iconPos[26] = {x=610, y=80}
iconPos[27] = {x=680, y=80}
iconPos[28] = {x=750, y=80}
iconPos[29] = {x=820, y=80}
iconPos[30] = {x=890, y=80}
iconPos[31] = {x=540, y=140}
iconPos[32] = {x=610, y=140}
iconPos[33] = {x=680, y=140}
iconPos[34] = {x=750, y=140}
iconPos[35] = {x=820, y=140}
iconPos[36] = {x=890, y=140}
iconPos[37] = {x=540, y=200}
iconPos[38] = {x=610, y=200}
iconPos[39] = {x=680, y=200}
iconPos[40] = {x=750, y=200}
iconPos[41] = {x=820, y=200}
iconPos[42] = {x=890, y=200}
iconPos[43] = {x=540, y=260}
iconPos[44] = {x=610, y=260}
iconPos[45] = {x=680, y=260}
iconPos[46] = {x=750, y=260}
iconPos[47] = {x=820, y=260}
iconPos[48] = {x=890, y=260}

iconPos2 = {}
iconPos2[1] = {x=70, y=80}
iconPos2[2] = {x=140, y=80}
iconPos2[3] = {x=210, y=80}
iconPos2[4] = {x=280, y=80}
iconPos2[5] = {x=350, y=80}
iconPos2[6] = {x=420, y=80}
iconPos2[7] = {x=70, y=140}
iconPos2[8] = {x=140, y=140}
iconPos2[9] = {x=210, y=140}
iconPos2[10] = {x=280, y=140}
iconPos2[11] = {x=350, y=140}
iconPos2[12] = {x=420, y=140}
iconPos2[13] = {x=70, y=200}
iconPos2[14] = {x=140, y=200}
iconPos2[15] = {x=210, y=200}
iconPos2[16] = {x=280, y=200}
iconPos2[17] = {x=350, y=200}
iconPos2[18] = {x=420, y=200}
iconPos2[19] = {x=70, y=260}
iconPos2[20] = {x=140, y=260}
iconPos2[21] = {x=210, y=260}
iconPos2[22] = {x=280, y=260}
iconPos2[23] = {x=350, y=260}
iconPos2[24] = {x=420, y=260}
iconPos2[25] = {x=540, y=80}
iconPos2[26] = {x=610, y=80}
iconPos2[27] = {x=680, y=80}
iconPos2[28] = {x=750, y=80}
iconPos2[29] = {x=820, y=80}
iconPos2[30] = {x=890, y=80}
iconPos2[31] = {x=540, y=140}
iconPos2[32] = {x=610, y=140}
iconPos2[33] = {x=680, y=140}
iconPos2[34] = {x=750, y=140}
iconPos2[35] = {x=820, y=140}
iconPos2[36] = {x=890, y=140}
iconPos2[37] = {x=540, y=200}
iconPos2[38] = {x=610, y=200}
iconPos2[39] = {x=680, y=200}
iconPos2[40] = {x=750, y=200}
iconPos2[41] = {x=820, y=200}
iconPos2[42] = {x=890, y=200}
iconPos2[43] = {x=540, y=260}
iconPos2[44] = {x=610, y=260}
iconPos2[45] = {x=680, y=260}
iconPos2[46] = {x=750, y=260}
iconPos2[47] = {x=820, y=260}
iconPos2[48] = {x=890, y=260}

–Load level icons accordingly
local function setupLevels()
for i = 1, 48 do
if tonumber(currentLevel) >= i then
level[i] = display.newImageRect(“moon.png”, 50, 50)
level[i].x, level[i].y = startX*i, 50
level[i].x, level[i].y = iconPos[i].x, iconPos[i].y
slideGroup:insert( level[i] )
level[i].scene = “level”…i…""
level[i]:addEventListener(“tap”, goLevel)
levelText[i] = display.newText(""…i…"", startX*i-2, 45, “Arial”, 20)
levelText[i]:setTextColor(0,0,0)
levelText[i].x, levelText[i].y = iconPos2[i].x, iconPos2[i].y
slideGroup:insert( levelText[i] )
elseif tonumber(currentLevel) <= i then
level[i] = display.newImageRect(“moonl.png”, 50, 50)
level[i].x, level[i].y = startX*i, 50
level[i].x, level[i].y = iconPos[i].x, iconPos[i].y
slideGroup:insert( level[i] )
levelText[i] = display.newText(""…i…"", startX*i-2, 45, “Arial”, 20)
levelText[i]:setTextColor(255,255,255)
levelText[i].x, levelText[i].y = iconPos2[i].x, iconPos2[i].y
slideGroup:insert( levelText[i] )
end
end
end
setupLevels()

local paintpage = 0
local dotpages = {}

paintpage = paintpage + 1

require(“movieclip”)
local dotpages = {}
local startdotsx = (480 / 2 - ((paintpage+1)*30)/2)
for i = 1, paintpage+1 do
dotpages[i] = movieclip.newAnim({ “slidedot_on.png”, “slidedot_off.png” })
dotpages[i]:play()
if (i == 1) then
dotpages[i]:stopAtFrame(1)
else
dotpages[i]:stopAtFrame(2)
end
dotpages[i].x = startdotsx + (i*30)
dotpages[i].y = 300
localGroup:insert(dotpages[i])
end

— DRAG SLIDE LEVEL SELECT
local startdragx = 0
local startgroupx = 0
local function startDrag( event )

local t = event.target
local phase = event.phase

if “began” == phase then
–display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
startdragx = event.x
startgroupx = slideGroup.x
elseif t.isFocus then
if “moved” == phase then
slideGroup.x = startgroupx-(startdragx-event.x)
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
local wopage = (-slideGroup.x) / 480
local newxi = math.round( wopage )
if (newxi > paintpage) then newxi = paintpage end
if (newxi < 0) then newxi = 0 end
transition.to( slideGroup, { x = -(newxi*480), time = 400 } )
for i = 1, paintpage+1 do
dotpages[i]:stopAtFrame(2)
if (i == newxi+1) then
dotpages[i]:stopAtFrame(1)
end
end
end
end
return true
end
bk:addEventListener( “touch”, startDrag )


function cleanGroups ( objectOrGroup )
if objectOrGroup.numChildren then
while objectOrGroup.numChildren > 0 do
cleanGroups ( objectOrGroup[objectOrGroup.numChildren])
end
end
objectOrGroup:removeSelf()
return
end

return localGroup
end [/code]
[import]uid: 17058 topic_id: 20995 reply_id: 320995[/import]

Use display.remove instead of removeSelf

display.remove checks first if the object is nil before trying to remove it.

This will solve duplicate removal errors (like your getting right now) [import]uid: 84637 topic_id: 20995 reply_id: 82994[/import]

@Danny Ok I got but whats weird I still get the same problem but It gets fix when you exit level select and enter in, I feel like is more like a glitch now, basically now it turned into a glitch well at least is not to bad thanks :slight_smile: [import]uid: 17058 topic_id: 20995 reply_id: 83015[/import]