|
| 24 Jul 2016 01:51 AM |
If I require() a ModuleScript from a LocalScript, will the ModuleScript only run locally? I'm laying out the groundwork for a proof of concept and I don't want to have to rewrite 8 scripts as localscripts with BindableFunctions.
Thanks! |
|
|
| Report Abuse |
|
|
gbroblox
|
  |
| Joined: 15 Jul 2009 |
| Total Posts: 21 |
|
|
| 24 Jul 2016 01:57 AM |
| A modulescript is basically just a giant function, and require(moduleScript) simply calls that giant function. If a modulescript is called from a script, it will run like a function called from a script (because it is, really). The same goes for a localscript. |
|
|
| Report Abuse |
|
|
gbroblox
|
  |
| Joined: 15 Jul 2009 |
| Total Posts: 21 |
|
|
| 24 Jul 2016 01:58 AM |
| *the same goes for calling from a localscript |
|
|
| Report Abuse |
|
|
pullman45
|
  |
| Joined: 09 Jul 2012 |
| Total Posts: 2891 |
|
|
| 24 Jul 2016 02:08 AM |
| How do you access values in a ModuleScript from a Script? |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2016 02:09 AM |
They return _exactly one_ value (whether that be a table, a function, et cetera), and the module code is executed by requiring them from a script (i.e calling the require function with the ModuleScript as the argument); and thus they run on the same machine they are required from. Keep in mind that the module code is only executed when they are are required, and the code will only be executed once per machine (i.e once on the server, once on the client).
I'm sorry if this is a bit of a bad explanation, though truthfully I don't really have a better way. Hope this helped at least. |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2016 02:14 AM |
"How do you access values in a ModuleScript from a Script?" My previous post was directed at OP, but here is an example for you (make sure you read my previous post to make sense of this):
-- ModuleScript ---------------
local module = {}
print("Hello world!") -- some random code to execute module.someNumber = 1337 -- Set a new property of table 'module'
function module:printUselessStuff() -- Declare a new method print("Useless stuff!") end
return module -- returns the 'module' table when required
-- Script ------------- local someModule = require(game.ServerStorage.SomeModule) -- calling the require global function with the desired modulescript as the argument
print(someModule.someNunber)
|
|
|
| Report Abuse |
|
|
pullman45
|
  |
| Joined: 09 Jul 2012 |
| Total Posts: 2891 |
|
|
| 24 Jul 2016 02:17 AM |
| Thanks for helping out. I'll see how that goes when I try that. |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2016 02:19 AM |
| BTW, there's a typo (print(someModule.someNunber) - it should be someNumber, not someNunber. |
|
|
| Report Abuse |
|
|
sioux123
|
  |
| Joined: 18 Nov 2011 |
| Total Posts: 970 |
|
|
| 24 Jul 2016 02:38 AM |
| could you just use a bindable and a regular script as an alternative to module script |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2016 02:42 AM |
| A bindable event isn't an alternative to a module script at all; you don't understand what a module does if you do. All a bindable event does is pass information from one script to another (running on the same machine, of course), and modulescripts: well, I've explained it already this thread. |
|
|
| Report Abuse |
|
|
sioux123
|
  |
| Joined: 18 Nov 2011 |
| Total Posts: 970 |
|
|
| 24 Jul 2016 02:44 AM |
I don't know much about modulescripts at all, all I know is that they can be used almost like an api(from what i know?)
I was saying you could have a script with functions in it, and bindablefunctions in it. Instead of using the modulescript just use the bindables? |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2016 02:49 AM |
A ModuleScript contains code and returns exactly one value; when the module is required in a script, all that code is executed and the value is returned. This is very different from passing information from one script to another (or in the case of BindableFunctions, hooking a function up to an event in one script and firing the event in another, for lack of a better phrase).
Look up modular programming. |
|
|
| Report Abuse |
|
|