Why i get this error?

I don’t know why this happens when i try to run this code:

main.lua:11:attempt to index global “button” (a nil value)

local buttons=display.newGroup() local loadscreen=display.newImage("screen.png") loadscreen.x=display.contentCenterX loadscreen.y=display.contentCenterY buttons:insert(loadscreen) local function game()   --stuff end button:addEventListener("touch", game)

button:addEventListener("touch", game)

should be:

buttons:addEventListener("touch", game)

And this one:

buttons=display.newGroup() --stuff local function playerItems(event) &nbsp; if ( event.phase == "ended" ) then &nbsp;&nbsp;&nbsp; if(t.item1==true)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item1=display.newImageRect("PNG/blaster\_icon.png",70,70) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item1=display.newImageRect("PNG/unknown\_icon\_1.png",70,70) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; b\_item1.x=280 &nbsp;&nbsp;&nbsp; b\_item1.y=800 &nbsp;&nbsp;&nbsp; if(t.item2==true)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item2=display.newImageRect("PNG/flamethrower\_icon.png",70,70) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item2=display.newImageRect("PNG/unknown\_icon\_2.png",70,70) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; b\_item2.x=360 &nbsp;&nbsp;&nbsp; b\_item2.y=800 &nbsp;&nbsp;&nbsp; buttons:insert(b\_item1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<--------------------&nbsp;attempt to index global&nbsp;'buttons'(a nil value) &nbsp;&nbsp;&nbsp; buttons:insert(b\_item2) &nbsp; end end

Forward declare buttons.

if i declare buttons inside of the function these lines won’t work

local function clearScreen(event) &nbsp;&nbsp;&nbsp; buttons:removeSelf() &nbsp;&nbsp;&nbsp; buttons=nil end buttons:addEventListener("touch", clearScreen)

forward declare buttons would look like this…
 

local buttons function playerItems(event) buttons = display.newGroup() --Stuff end local function clearScreen(event) buttons:removeSelf() buttons = nil end

as long as your playerItems function runs before the clearScreen function, it will work for you

button:addEventListener("touch", game)

should be:

buttons:addEventListener("touch", game)

And this one:

buttons=display.newGroup() --stuff local function playerItems(event) &nbsp; if ( event.phase == "ended" ) then &nbsp;&nbsp;&nbsp; if(t.item1==true)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item1=display.newImageRect("PNG/blaster\_icon.png",70,70) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item1=display.newImageRect("PNG/unknown\_icon\_1.png",70,70) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; b\_item1.x=280 &nbsp;&nbsp;&nbsp; b\_item1.y=800 &nbsp;&nbsp;&nbsp; if(t.item2==true)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item2=display.newImageRect("PNG/flamethrower\_icon.png",70,70) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b\_item2=display.newImageRect("PNG/unknown\_icon\_2.png",70,70) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; b\_item2.x=360 &nbsp;&nbsp;&nbsp; b\_item2.y=800 &nbsp;&nbsp;&nbsp; buttons:insert(b\_item1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<--------------------&nbsp;attempt to index global&nbsp;'buttons'(a nil value) &nbsp;&nbsp;&nbsp; buttons:insert(b\_item2) &nbsp; end end

Forward declare buttons.

if i declare buttons inside of the function these lines won’t work

local function clearScreen(event) &nbsp;&nbsp;&nbsp; buttons:removeSelf() &nbsp;&nbsp;&nbsp; buttons=nil end buttons:addEventListener("touch", clearScreen)

forward declare buttons would look like this…
 

local buttons function playerItems(event) buttons = display.newGroup() --Stuff end local function clearScreen(event) buttons:removeSelf() buttons = nil end

as long as your playerItems function runs before the clearScreen function, it will work for you