Log in to vote

ModuleScripts: How do they work?

Asked by
Dummiez 360 Moderation Voter
8 years ago

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

Log in to vote
Answered by 8 years ago

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)

return MyModule
Will you be able to call it multiple times from the same script? Dummiez 360 — 8y
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? ColdSmoke 55 — 8y
Log in to vote
Answered by 7 years ago

'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.)

Answer this question