problem with the scope of variables?

Hello!
I’m trying some code using the director class.
I’ve work with 2 scenes (scene1, scene2).
Please, pay attention to this: At scene1 I press a button, go to scene2, press another button at scene2 and come back to scene1, press the same button go again to scene2 and finally press another button that show me an image.

With this code everything is ok:

module(..., package.seeall)  
local ui = require("ui")  
local scrollNav = require("scrollNav")  
local contido = require("contidoSlider")  
  
local localGroup = display.newGroup()  
local sliderPrincipal  
local sliderSecundario  
local slider2  
  
local ancho,alto=display.contentWidth,display.contentHeight  
local fondo=display.newImage("fondo.jpg")  
local accionInicio = function (event)  
 if event.phase == "release" then  
 director:changeScene("escena1","moveFromLeft")  
 end  
end  
  
local function elementoElexido(event)  
 slider2=display.newGroup()  
 local sliderSecundario=display.newImage("fondo\_thumb.png",0,0)  
 slider2:insert(sliderSecundario)  
 slider2.x=0  
 slider2.y=alto-150  
 localGroup:insert(slider2)  
end  
  
--Buttons  
local boton\_inicio = ui.newButton{  
 default = "bot\_inicio.png",  
 over = "bot\_inicio.png",  
 onEvent = accionInicio,  
 emboss = true  
}  
local boton\_editar = ui.newButton{  
 default = "bot\_editar\_es.png",  
 over = "bot\_editar\_es.png",  
 onEvent = elementoElexido,  
 emboss = true  
}  
local function initVars ()  
  
 -----------------------------------  
 -- Insercións  
 -----------------------------------  
  
 localGroup:insert(fondo)  
 localGroup:insert(boton\_inicio)  
 localGroup:insert(boton\_editar)  
  
 -----------------------------------  
 -- Posicionamentos  
 -----------------------------------  
  
 boton\_inicio.x=40  
 boton\_inicio.y=45  
 boton\_editar.x=40  
 boton\_editar.y=alto-45  
  
  
end  
  
function clean ( event )  
 --print("escena 2 limpa")  
end  
  
---------------------------------------------------------------  
-- Todas as escenas deben ter esta función NEW  
---------------------------------------------------------------  
  
function new()  
  
 -----------------------------------  
 -- Inicializo variables  
 -----------------------------------  
  
 initVars()  
  
 return localGroup  
  
end  
  
Runtime:addEventListener("scrollNav.elemElexido", elementoElexido)  

But when I try to use a new function navegadorPrincipal(), the code crashes.
With this function, I use a scrollNav class instead a button to show the image.
When I go to this scene at the first time, everything works ok, but if a go back to previous scene and then come again to this second scene, the code crashes.
Works fine the first time, and crashes then.

module(..., package.seeall)  
  
local ui = require("ui")  
local scrollNav = require("scrollNav")  
local contido = require("contidoSlider")  
  
local localGroup = display.newGroup()  
--local slider2=display.newGroup()  
local sliderPrincipal  
local sliderSecundario  
local slider2  
  
local ancho,alto=display.contentWidth,display.contentHeight  
local fondo=display.newImage("fondo.jpg")  
  
local function navegadorPrincipal()  
 sliderPrincipal = scrollNav.new({left=0, right=0, tm=422, lm=0, sp=0, altoS=50})  
 for index, value in ipairs(contido) do  
 local thumb = display.newImage(contido[index].thumb)  
 sliderPrincipal:insertButton(thumb, contido[index].asset)  
 end  
  
 return true  
  
end  
  
navegadorPrincipal()  
  
local accionInicio = function (event)  
 if event.phase == "release" then  
 director:changeScene("escena1","moveFromLeft")  
 end  
end  
  
local function elementoElexido(event)  
 slider2=display.newGroup()  
 local sliderSecundario=display.newImage("fondo\_thumb.png",0,0)  
 slider2:insert(sliderSecundario)  
 slider2.x=0  
 slider2.y=alto-150  
 localGroup:insert(slider2) -- At this line crash the code with: attempt to call method 'insert' (a nil value)  
end  
  
--Buttons  
local boton\_inicio = ui.newButton{  
 default = "bot\_inicio.png",  
 over = "bot\_inicio.png",  
 onEvent = accionInicio,  
 emboss = true  
}  
local function initVars ()  
  
 -----------------------------------  
 -- Insercións  
 -----------------------------------  
  
 localGroup:insert(fondo)  
 localGroup:insert(boton\_inicio)  
 localGroup:insert(sliderPrincipal)  
  
 -----------------------------------  
 -- Posicionamentos  
 -----------------------------------  
  
 boton\_inicio.x=40  
 boton\_inicio.y=45  
  
end  
function clean ( event )  
 --print("escena 2 limpa")  
end  
  
---------------------------------------------------------------  
-- Todas as escenas deben ter esta función NEW  
---------------------------------------------------------------  
  
function new()  
  
 -----------------------------------  
 -- Inicializo variables  
 -----------------------------------  
  
 initVars()  
  
 return localGroup  
  
end  
  
Runtime:addEventListener("scrollNav.elemElexido", elementoElexido)  

Could anybody tell me why notifies me the error on the elementoElexido function?
What am I doing wrong?

TIA [import]uid: 44013 topic_id: 9854 reply_id: 309854[/import]

If the variable localGroup is global instead of local, no runtime error…
Could anybody tell me the reason?

Sorry if I don’t explain good! [import]uid: 44013 topic_id: 9854 reply_id: 36115[/import]