How to make groups visible across lua files?

I have my main game.lua file which has another gameplay.lua file.

In gameplay.lua I try to insert objects into mainGroup which was created in game.lua but I get this error:

…vz07_0x4fkl00000gn/T/TemporaryItems/10/gameplay.lua:15: attempt to index global ‘mainGroup’ (a nil value)

[lua] – Create and display the HUD
createHUD = function()
– Add HUD graphic
local theHUDBanner = display.newImageRect ( “HUDHeader.png”, 480, 35 )
theHUDBanner.x, theHUDBanner.y = 240, 12
mainGroup:insert(theHUDBanner)[/lua]
The ‘insert’ is on line 15 [import]uid: 10389 topic_id: 21313 reply_id: 321313[/import]

Is the mainGroup variable persisted across your modules? if not, then it gets the default value of nil and hence this issue. [import]uid: 3826 topic_id: 21313 reply_id: 84425[/import]

Well it’s defined in game.lua and is global. Doesn’t that make it persist across modules? [import]uid: 10389 topic_id: 21313 reply_id: 84514[/import]

no its only global to that module
you can pass it to the other module

othermodulefunction(group)

then in other module

function othermodulefunction(arg)

groupfromothermodule = arg [import]uid: 7911 topic_id: 21313 reply_id: 84523[/import]

Oh! Okay. Thanks for your help guys! [import]uid: 10389 topic_id: 21313 reply_id: 84535[/import]

Try this

        -- Create and display the HUD          createHUD = function()                 -- Add HUD graphic                 local theHUDBanner = display.newImageRect (  "HUDHeader.png",  480, 35 )                 theHUDBanner.x, theHUDBanner.y = 240, 12                 game.mainGroup:insert(theHUDBanner) [import]uid: 11559 topic_id: 21313 reply_id: 85988[/import]