How to access the control table of the function "require"?

See this for an explanation of what I mean with “control table” Programming in Lua : 8.1

I made a thread once: Why does a ModuleScript requiring a specific returned value from another ModuleScript not update when reloading the script?

Where I asked that if I had some ModuleScript that, I don’t know… it calls another ModuleScript that returns a value uhhh…

Well, I wasn’t able to get a updated result when reloading that specific module.

 

Then recently I found out that you can use require() and provide it with an AssetID. This then will attempt to load a Model but specifically look for a ModuleScript called “MainModule”.

So you can do require(assetid) and yeah. Thing is, when you update this Model with the “MainModule”, using require(assetid) with the same assetid, won’t load the updated version.

I have to restart Studio or the game incase I used require() while starting the game.

So, I started thinking on why that would be so.

 

And the Lua docs say that require, well Lua’s native require has a control table, that is accessible through a global value called _LOADED, so something similar like _VERSION

just that _LOADED doesn’t seem to be accessible, and I was looking for a way that does a complete reload on everything that was done with require

wonder if it is possible

1 Like

The library asset needs to be named ‘MainModule’ not ‘MainScript’.

Additionally, the ‘loaded’ table is not provided by Luau, you could emulate its behavior by having a script require every ModuleScript and packing all of the return values within a single dictionary (module names as keys/fields and their contents as the respective values).

1 Like

It seems like something that you could write a bug report for. Not sure if it is really intended behaviour considering it’s quite a niche use case.

1 Like

It’s not a bug, the entire ‘package’ library which is native to Lua has been omitted from Luau.

Hence why the package.loaded, package.preload and package.searchers tables are not defined.

In native Lua package.loaded contains a cache of the loaded modules, package.preload contains user-defined functions for loading modules and package.searchers contains ‘searcher’ functions that are used to locate a suitable ‘loader’ function for the required module.

The behaviour within the command line of studio seems like a bug if you can’t get it to refresh the module without restarting studio.

Oh my bad, I wasn’t sure what you were referring to. This is a fairly well documented issue.

The work around is to just launch a testing session of the game, or delete the ModuleScript and re-create it. Another approach is to convert the module into a constructor function which returns an object that inherits all of the module’s functions and properties.

local Module = {}

function Module.new()
	return setmetatable({}, {__index = Module})
end

return Module

Each call to Module.new() will return a new object that inherits the module script’s contents.

2 Likes

How would it work with the constructor method? I tried to do something but it didn’t seem to work.

Also if the MainModule from require() even deletes itself it still will give me the previous version.