Is it okay to require ModuleScripts inside functions?

I’m a little bit worried since I feel like requiring a ModuleScript takes a bit of performance consumption. And the idea I think behind it is that when requiring a ModuleScript, it returns the entire data it contains. Which could be 1500 lines of code, that’s enormous.

So is it okay to require ModuleScripts then immediately run it during a critical client data calculation? When I mean critical client data calculation, I mean data the client absolutely needs like substracting and adding the amount of currency they have.

Yes I know Servers should be the only thing that touches data like that. I need it for other stuff.

require caches the result of ModuleScripts, so if you require the same module 20 times let’s say, you only execute its code once. So subsequent calls would be faster. If you can though, require the module first thing, so it’s just one call at the top and the result variable is an upvalue in your functions.

1 Like