Can somebody explain to me why scripts still run after being disabled if you require a module with no return (it just runs the code inside)?
This only happens without a return. This seems to be like an engine bug because ModuleScripts don’t do anything. Require() is supposed to act similar to loadstring() in that sense. Apparently this bug has existed since the modules have existed. Is there a reason this hasn’t been patched yet or is this actually supposed to be how it works? I highly doubt it is as all scripts capable of running have a Disable property that is supposed to disable the script. It just wouldn’t make any sense for it to not work this way,
(Sorry, mild rant because I seem to constantly run into weird Roblox problems). It’s not even that this really matters all that much, but even so, I don’t like when things work a way that they shouldn’t.
Is anything un-disabling it when the game runs? If not, I recommend creating a bug-report within #bug-reports.
No, this is with a blank baseplate. I’m just wanting to confirm that this is a bug before posting there. (Although I worry that nobody will read it there because it rarely seems to be the case).
Well by default, when you require a module, the code in it will run, and most likely run until the end of time, they can’t be disabled.
About regular scripts, I don’t get how they are disabled as they’re like the propagators, but I assume they do.
It’s how they’re supposed to work, Modules are like a essential library, but every programming language is different. In Lua, Modules run forever, but can only be ran by require(), it can’t be stopped unless something in the code yields the Module.
With Roblox, they make Lua’s require function cache anything it loads for some reason.
1 Like
Is that really how it’s supposed to work? I thought modules were supposed to essentially be nothing but text holders. I consider require() to be similar to loadstring() in the sense that it gets the text from the module and executes it like loadstring does. That would only make sense to me.
Edit:
After some quick research, it appears that things work slightly differently than I thought:
A ModuleScript is a type of Lua source container that runs once and must return exactly one value. This value is then returned by a call to require
given the ModuleScript as the only argument. ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require
.
I still feel like that doesn’t really explain why it works that way. It almost seems like an oversight to me considering it says “must return exactly one value”. But I suppose that could be specifically for if you want to return something in the first place.
Edit 2:
Note that the first call to require
on a ModuleScript will not yield (halt) unless the ModuleScript yields (e.g. calls wait
). The current thread that called require
will yield until a ModuleScript returns a value. A run time error is generated if this doesn’t happen. If a ModuleScript is attempting to require
another ModuleScript that in turn tries to require
s it, the thread will hang and never halt (cyclic require
calls do not generate errors). Be mindful of your module dependencies in large projects!
I guess this answers my question, but yikes.
1 Like