Using localGroup:insert on external modules?

Ive decided to take a look at the director class and am having troubles understanding how to do what Im trying to do. I have a character that can run around screen and touch things. Before I decided to use the director class, I had a version that was capable of touching a money object and have it disappear. Simple. Now that Im trying to use director, things get a little complicated.

So I understand that Im supposed to place everything in localGroup for removal reasons, right? The problem is that Im not sure how Im supposed to go about this for these simple external lua files like this one.

Here are excerpts of my pre-director code from the game.lua

local money = {}  
  
local newMoney = require("money");  
  
local function spawnMoney(x,y)  
 local m = newMoney.new();  
 money[m] = m  
 money[m].x = x  
 money[m].y = y  
 money[m].remove = true;  
end   
  
spawnMoney(300,250);  
spawnMoney(200,200);  

And now from money.lua

  
module(..., package.seeall);  
  
 function new()  
 money = display.newImage("green.png", 0, 0)  
 physics.addBody ( money, "static", {isSensor = true} )  
  
 function getMoney(self,event)   
 if event.phase == "began" and event.other.type == "player" then  
 print ("$$$")  
 money:removeSelf()  
 elseif event.phase == "ended" and event.other.type == "player" then  
 end  
 end  
  
 money.collision = getMoney  
 money:addEventListener( "collision", money )  
  
 return money;  
end  

The purpose is to jump, touch, have it disappear and print $$$ in the terminal. It worked fine pre-director, but now after numerous attempts at getting it to work, the best I can do is have the money at (200,200) disappear, and then it will throw me a “attempt to call method ‘removeSelf’(a nil value)”.

Ive tried to put everything I can think of into the localGroup both inside game.lua and money.lua one at a time, to no avail. Anyone have any ideas as to what Im doing wrong? [import]uid: 67886 topic_id: 11406 reply_id: 311406[/import]

Whenever you are creating variables or tables you have to declare them after requiring any external modules otherwise they won’t be visible to the required module(s). So in other words try changing this:[lua]local money = {}

local newMoney = require(“money”);[/lua]
to this:[lua]local newMoney = require(“money”);

local money = {}[/lua]
[import]uid: 27965 topic_id: 11406 reply_id: 41358[/import]

Hmm, I havent tried this out with the director version yet but what exactly is the purpose of “local money = {}”? To be honest, I think I got the code from a cheetomoskeeto tutorial video but I cant seem to find which one right now.

If I comment out that particular line in the original(the one that worked), everything still seems to work… Hmm, I have much to learn [import]uid: 67886 topic_id: 11406 reply_id: 41366[/import]

[lua]local money = {} – this line creates an empty table.
– By declaring it before the require statement below the
– required module (money.lua) cannot see or use it.

local newMoney = require(“money”);
local function spawnMoney(x,y)
local m = newMoney.new();
money[m] = m
money[m].x = x
money[m].y = y
money[m].remove = true;
end

spawnMoney(300,250);
spawnMoney(200,200); [/lua] [import]uid: 27965 topic_id: 11406 reply_id: 41375[/import]