When I use require(123), where does the required module go, and how can I require it to ServerScriptService?
If you require something locally, it’ll be accessible locally. If you require something server-sided, it’ll be accessible server-sided. Basically the equivalent of _G
or shared
tables if you’re familiar with those. The value returned by require
is then stored into the variable, like so-
local someTable = require(myModule)
someTable.add(1, 3)
require
works in two ways. One, it can pull from an existing module you have in your game, in that scenario you’d pass in the module instance. Or, if I’m not mistaken, you can actually pass in the ID of a module that you own, and that’ll work as well. I’m not entirely sure how that works but I’m certain it’s possible.
Hope this helps.
Its requiring a module from its asset ID in a serverscript in ServerScriptService. But where will the module go when its required if its an asset id?
When you require something like you make a variable = to require(module) your value will be equal to that whole module and you can require the same module from multiple scripts you module doesn’t go anywhere
Im requiring a module by its asset ID. When I do that does it get added to the game?
No you just using it’s scripts
On the Wiki it specifies requiring asset IDs is a way of creating “private” modules, so I’m guessing it never actually inserts the ModuleScript object in the game; rather just pulls the code from it.
When you require a module by its AssetId, the returned value is cached in memory. Any code in the main scope will also be executed in the game environment via VM. The script instance, as mentioned above, is never actually inserted into the DataModel.
I do not believe it is possible to grab a reference to the script instance, and if it was, that method probably isn’t available anymore or doesn’t matter that much given the change in security for requiring a module by id.