How to run a piece of code when it is called in a ModuleScript

For a product loader I am making, I don’t want someone to just resell the script and say it is their own one. So I came up with the idea of a module script to load it all in for them. However, I want the loader to not be messy with local loader = require(123456789) loader.start. So how do you just do require(123456789) without a run function script?

I’m not sure exactly what you mean by this. By calling “loader.start”, it is running a new command that is separate from the requiring of the module.

If you define the require module as a variable as you are doing, you can call it anywhere later in your script.

I’m not sure what your saying but can’t you just run code inside the module? The code will only run the first time you require the module, this is what I do for my projectileclass.

So, you can just slap some code in it, require the module and it runs the code?

Yah it does that. That’s what happens in module scripts. Try it.

In a module script do:
print “hello world”

And require it, and it will print the hello world. If you require it again it won’t run again since modules only run once. And after that return the reference of the table. (Which is probably what you want since you mention loading.

Oh, I did not know this! Thanks for the information! I am quite clueless in some areas since I am a JavaScript programmer (hence my tag).

1 Like