Question about modules

Hello!

I have this somewhat specific scenario, I have a module which, on require, should create a table of all the buttons, however I was wondering if the table that is returned is an actual reference by the table that is inside of the module that can be modified by that same function?

For example, this is my module:

local module = {
    ['Table'] = (function()
        local table = {}
        workspace.DescendantAdded:Connect(function(descendant)
            table[#table + 1] = descendant
        end)
        return table
    end)()
}
return module

Now, after the first require, it looks like this:

local module = {
    ['Table'] = {}
}
return module

Now, if I fire the event in the first code sample, would it modify the table inside of the second code sample?

Every subsequent require on a module after the first one returns the same instance provided its required on the same environment. The server, client and command bar each receive their own instances of a module upon an initial require. If you return a table you can directly mutate that table and all other scripts of the same environment requiring the module will see the change.

1 Like