Do You Define Module Properties Inside the Table or After? What’s Better?

Hey everyone,

I’ve seen different styles when it comes to defining properties inside a Lua table (like a module or manager) in Roblox development, and I’m curious what other devs prefer and why.

Option 1 – Define everything inside the table upfront:

local ZombieManager = {
	ActiveZombies = {}
}

return ZombieManager

Option 2 – Create the table first, then add properties:

local ZombieManager = {}
ZombieManager.ActiveZombies = {}

return ZombieManager

Both seem to work the same in practice, but I’m wondering:

Is there a best practice in the Roblox Lua community for readability, organization, or performance?
Are there scenarios where one is clearly better than the other?

Would love to hear your thoughts especially from anyone working on modular systems or large-scale games!

Probably just up to preference.
Sometimes I prefer the former as it makes the structure clearer, or if structure is not that important and I’m just sectioning off something like a configuration table, I do something like this:

local CONFIG = {}

CONFIG.ThingA = {
	foo = "bar",
	abc = "def"
}

CONFIG.ThingB = {
	cat = "meow"
}

Also this is probably meant to go into Development Discussion

2 Likes

As @nowodev said its mostly preference.

However Option 2 does one thing unique in code vs number 1. It triggers the __index call in table therefore you can use metatables with it.

An example of use can be automatically filling the table up with random values or detecting duplicate entries because of a developers mistake (me).

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