Empty initialized table in ModuleScript

Hi there.

I have a hierarchy something like this:

----- ReplicatedStorage
--- ModuleScript

----- StarterPlayer
--- StarterPlayerScripts
-- LocalScript

Within the module script, I initialize a table called foo:

local module = {}

module.foo = {
    ["bar"] = module.foobar
}

function module.foobar() end

Within my LocalScript, I access the module:

local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")
local module_script: ModuleScript = require(RS:WaitForChild("ModuleScript"))
print(module_script)

--> {
-->    ["foo"] = {},
-->    ["foobar"] = function (hex address)
--> }

The issue is that the table foo declared in the ModuleScript appears empty when accessed in the LocalScript, whereas the expected output is:

print(module_script)

--> {
-->    ["foo"] = {
-->         ["bar"] = function (hex address)
-->     },
-->    ["foobar"] = function (hex address)
--> }

module.foobar is not yet declared when you index it with "bar", so the key gets removed. You could either declare the function beforehand or index with a lambda function directly.

That fixed things nicely, thanks! I forgot that Lua references based on assignments declared earlier in the script!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.