Questions about modulescripts and best practices

  1. What do you want to achieve? Keep it simple and clear!
    Learn why we use module scripts and when to use them
  2. What is the issue? Include screenshots / videos if possible!
    I do not know the best way of having scripts (see below for less confusion)
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have seen many posts on this topic, but most of them come out to function replication, which I will go over what doesn’t make sense to me about it in a second.

So recently I realized that I had 10 ish scripts doing different things in serverscriptservice. I thought why would I have all of those doing things and the possibilities of making them communicate through remote events if I can just put them in one script? So I put all my local scripts in one local script and all my server scripts into one script.
Screenshot (695)

Screenshot (694)

This is working perfectly fine, but then I remembered module scripts and I wondered if they were a better approach. Should I have a bunch of module scripts doing different functions that can be called from with one script require() ing each and calling the functions with events? Should I have one module script being called by a regular script with all the functions? Should I keep all the functions in one server script? etc. I am just questioning what the best approach is, even though they all work. My second question is why use module scripts? Like I said, most of the former posts came out to stuff like function replication (Not including OOP here), which I don’t quite get. If you are making a game why would you want more than 1 script when you can do everything on one only? Thanks for your time if you read all this.

I’ll give the best rundown that I think answers your questions.

The reason to have multiple functions in multiple scripts, especially with more taxing functions that may create and edit objects rapidly, is so that you don’t have one “choke point” script. Every script requires memory on the server to run, you can see this breakdown in the Scripts tab of the Developer Console.

Personally I keep function Module Scripts in a folder with all my other Server Scripts in the ServerScriptService, so it’s easier to reference with script.Parent.ModuleScript. I keep Module scripts that I use for data and tables in ReplicatedStorage so the Client can access them too

Module scripts can be used as an alternative for Server Scripts in ServerScriptService, but when you require and run a function through them, it throttles the script that is requiring the module, meaning you have to wait for the function to end in the Module Script before the regular script’s code can continue.

I personally use Bindable Events if I’m doing things like creating a part, effect, etc. where it does something and doesn’t calculate or retrieve something. I use Module scripts for commonly used functions which get data or calculate things, in which case the function would need to return a value back to the script.

Let me know if you need anything else cleared up

Thank you for your response. I didn’t know the client could access module scripts in replicated storage. I also didn’t know that it waits for the modules scripts function to end. I’ve never used bindable events, maybe it’s time to try.

1 Like

module scripts are just a way of keeping things nice and tidy.

if you’re starting to realise that the same function is being used in different scripts, that’s when you should use a module script. that way, if you want to change something in the function, you just have to change it from the module script.

since you’ve put all ur code in one script, ig there’s isn’t much of a need for a module script.

Thanks for the reply, will take this into account.

1 Like