How would I get the module from itself

I have a Commands module script that has some commands with descriptions and functions. This is how it looks like:

local Commands = {
	["cmds"] = {
		desc = "Prints all the available commands.",
		func = function(plr)
			for _, cmd in pairs(script) do
				
			end
		end,
	}
}

return Commands

Now apparently Commands cannot be accessed in the script, so I tried, as you can see, script. I don’t think this will work, so what can I do about this? Please don’t tell me I have to create another table…

make commands a global variable by removing local and put “Commands” instead of script (without the “”)

for _, cmd in pairs(Commands) is more correct

If you get any warnings from Commands not being defined yet then make Commands a global variable

I am not sure that for _, cmd in pairs(require(script)) would work because that might result in infinite recursion

Actually that resulted in an error saying “expected table, got Instance”

Thanks to you too :smiley:

pairs(script) gives the ‘expected table, got Instance error’ because you are supposed to use require() to import modules, but using require(script) would cause infinite recursion like making a function that calls itself

1 Like

Instead of making it global, you should forward-declare it.

local Commands
Commands = {
	["cmds"] = {
		desc = "Prints all the available commands.",
		func = function(plr)
			for _, cmd in pairs(Commands) do
				
			end
		end,
	}
}

return Commands

That doesn’t work, I have tried it. It says that Commands is nil.

That does work:

Weird, it didn’t work for me. Oh well, what can you do.

1 Like