Have module scripts index the scripts which require it?
No because multiple scripts can require a module but the module only runs once and therefore only has one script reference. It would be misleading and generally unusable, two things no API should ever be.
I’m sorry… But how? :shocked: :?
It’d just be a table stored in the properties, or returned when you call a method on it… Like… :GetRequirees() or something.
Modules don’t magically know every script that’s ever going to require them. Think of it as a timeline:
1) Script A requires Module
2) Module executes
3) Script B requires Module
4) Module doesn't execute because it already executed, it just returns that same value
The problem here is that you obviously want to have different cases depending on which script is requiring your module, but since the module only runs once (when it’s required the first time) it will only ever be aware of a single script.
The solution to your problem is to return a function that then executes whatever code you want based on whatever requirements it’s given.
return function(requiree)
-- stuff
end
require(Module)(script)
If you need to know that information then you are doing something wrong.
If you’re trying to create a module that does something to the scripts that call on it, then you should structure the code in question as having the thing happen when the scripts call one of the module’s functions, not on require. That’s because, as mentioned above, calling require doesn’t necessarily do anything, it may just return the module without running any code if the module is already loaded.
local StuffInjector = {}
function StuffInjector.Inject()
getfenv(0).blahVar = ...
end
return StuffInjector
require(StuffInjector).Inject()
print(blahVar)