From a script, the require function can now use a string, and it will require the first ModuleScript it can find with the specified name.
local module = require("ModuleScript")
This would allow us to make a cleaner looking code base, since we wouldn’t have to worry about having to make ugly long lines of WaitForChild code to fetch a module that multiple scripts in my game are using, and we wouldn’t have to get confused by a vague MainModule assetId parameter that we don’t want to memorize.
In the meantime, it’s actually fairly easy to create something similar by yourself:
Insert a Folder and use the command bar to reparent it to ‘game’.
Rename the Folder to the desired service name.
Fill your Folder with ModuleScripts and sub-folders which also contain ModuleScripts
Create a Script somewhere (ServerScriptService?) and define a global variable which is a function that recursively finds an item with the given name in your Folder.
Now you can use something like this in all your Scripts:
local YourModule = require(_G.LookForModule("SomeRandomModule"))
There is also the option of overriding the default require function:
-- In RequireModule.lua
return function(name)
local module = game.ServerStorage.Modules:FindFirstChild(name)
if module ~= nil then
return require(module)
end
return nil
end
Then you can do this:
local require = require(game.ServerStorage.Modules.RequireModule)
-- Other modules can use the "nice" syntax
local testModule = require("TestModule")
local otherModule = require("OtherModule")
Yeah, this is nice because you don’t have to use a global and there’s no chance that you’re using the require method before it’s being initialised, but personally I prefer the _G method (just in this instance) because it doesn’t add clutter, and I have my code set up in such a way that the _G.require is always initialised before any other module can run.
I would only use this if it recursively searches for modules. If it just requires at the top level by name, or if it requires by folder structure by name, then it’s near-pointless. We already do that now. By making it require recursively it is possible for devs to choose to require directly (require(game.ModuleService.FolderTop.FolderSub.ModuleName)) or by recursive search (require("ModuleName")).
I currently use my own system that allows for recursive search by module name. It works very well and helps a lot as a code-base grows and needs more organization without disrupting existing programming.
Because people want folders, but instances can be called “a/b”, maybe have this syntax:
require(Instance.new("ModuleScript")) -- default
require("Module") -- look for "Module" in the ModuleScriptService
require("Folder","Module") -- look for MSS.Folder.Module
require({"Folder","Module"}) -- same as ^
That also allows for
require("what/ever","you/want","Module")
require{"what/ever","you/want","Module"}
--> look for MSS["what/ever"]["you/want"].Module
Because we don’t want “Module is a valid member of Folder”:
If the last argument (or last element in the table argument) is a number: use WaitForChild internally (we could also WaitForChild forever, which is a bit weird. same for waiting a default time) (most logical would be that without that number, it does the error stuff)
Or we could just take all of the people with stupid instance names and move them over there
In all seriousness, I don’t see why we need to design this around people doing dumb stuff – that’s like a different form of premature optimization. If someone doesn’t want errors they shouldn’t use stupid instance names. If they do run into issues they can debug and adjust like a normal person.
They were deleted for a reason. Bringing up deleted posts just continues what they were deleted to stop (off-topicness of thread). PM someone if you want to know what the posts were (this is applicable to anyone on any thread).