Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

ModuleScripts: How do they work?

I've read the wiki about ModuleScripts, but it isn't making sense. I mean, do ModuleScripts have to be stored in Workplace or ServerScriptService? (cause the examples are in the Workspace) To me, it reminds of Java and importing libraries.

Is there is difference to it with using Global Functions? And when it states that ModuleScripts will only run once, does that mean:

  • it can no longer be used by any other function again, until the process is complete?
  • it can't be used by any other script after it is called?

Also, limitations to ServerScripts and LocalScripts accessing ModuleScripts? (No examples of ModuleScripts being using in Roblox Wiki)

local printModule = require(game.Workspace.MyModuleScript)

printModule("Test Print") -- prints "Test Print" to output
printModule("Test Print") 
printModule("Test Print") 

Something like this, for example can be called?

2 answers

I've been using Module scripts a lot recently. I usually just leave mine in the workspace form old habit, but I'm sure they work from serverscriptstorage too.

I've set up modules before so that they contain loops, with hundreds of parts calling the same function and it works perfectly fine.

When calling functions from the Module Script you need to do something like this:

local printModule = require(game.Workspace.MyModuleScript)

printModule.printFunction("Test Print") 

where printModule looks like:

local printModule = {}
function printModule.printFunction(string)
      print(string)
end

return MyModule

2 comments

0

Will you be able to call it multiple times from the same script?

0

You should be able to, however I haven't tested it so I'm not sure. The code I posted is functional, so you could do some tests with it and share your results?

'it can't be used by any other script after it is called?'

They can be called as many times as you wish but it will ALWAYS return the same result for things like math.random(INT, [INT])

(I advise you make ColdSmokes Answer the Answer for the thread.)

This discussion is preserved as it was. New answers, comments, and votes are closed.