I have a 900 lines block of code that runs 6 functions on each client. Should I use module scripts?

Rather, more than just answer with yes or no, I want to know when I should use a Modulescript.

So the thing is something like this

In a local script

    Local function dothing1()
- - blablablablablabla
End

    Local function dothing2()
- - bla bla, but now it's different
End


   Local function dothing3()
- - something else
End

  Repeat
  Event.OnclientEvent:Wait()
  dothing1()
  dothing2()
  dothing3()
  Wait(1)
  Until nil

This runs on every single client and has to initiate it every time. I wasn’t planning on using module scripts because Im not running just one function, I’m running 5, then I discovered that there’s functions inside functions and I can just return one with all of them, and I feel stupid.

So

Is this right to use a module script?

Where should I put it?

How can I pass more than one function in one module script?

1 Like

Use Module Scripts to store information that affects gameplay but isn’t changed by the client, also use module scripts to store functions that are to be reused for example
I have scripts that all need to know for how long it should stun a player

Example
-- Module Script
return {
   Seconds = 5
}

-- In Combat Script 1 (pseudocode)
local CombatSettings = require(path_to_module)

if got hit then 
    play knockback effect
    if i took critical damage then
        stun myself out for CombatSettings Seconds
        delay(CombatSettings.Seconds, function()
            come back to my senses
        end)
    end
end

-- In NPC Script

local CombatSettings = require(path_to_module)

if NPC hit player then
    if critical hit then
       stun player 
       delay(CombatSettings.Seconds, CombatSettings(function()
           stop NPC 
           respawn NPC
           bring player back to life
       end)
end

Module Scripts really shout abstraction, reusability, and configuration. In your case, if all three of these functions principally have the same behaviour, then yes, it would sense to use a module for that:

Another Example
Module Script
local ThumbnailComponent = {}

local Players = game:GetService("Players")

function ThumbnailComponent.GetUserThumbnail(Player, ThumbnailType, ThumbnailSize)
     local Thumbnail = Players:GetUserThumbnailAsync(
        Player.UserId,
        ThumbnailType,
        ThumbnailSize,
       )
     local ThumbnailDisplay = Instance.new("ImageLabel")
     ThumbnailDisplay.Size = UDim2.new(0, 200, 0, 200)
     ThumbnailDisplay.Image = Thumbnail
     ThumbnailDisplay.Name = Player.Name.."'s Thumbnail"
     return ThumbnailDisplay
end

return ThingComponent
Client Script
-- What you kind of have here is

GenerateThumbnailEvent:Connect(
     function(Style)
          if Style == "Avatar" then
               generatesomethingfunction(bla, bla, bla)
          if Style == "Something Else" then
               generateAnotherFunction(bla, bla bla)
          end
     end
)

--  With Module Script

local ThumbnailComponent = require(path_to_module)
GenerateThumbnailEvent:Connect(
    -- How about we send in our information and have the function handle it and change its behaviour accordingly
     function(Player, EnumType, EnumSize)
         local Thumbnail = ThumbnailComponent.GetUserThumbnail(Player, EnumType, EnumSize)
     end
)


2 Likes

Essentially what you want to achieve with modularisation is to be able to reuse information efficiently, without having to rewrite everything with a slight change in behaviour