generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Small question on ModuleScripts

Previous Thread :: Next Thread 
Macelarius is not online. Macelarius
Joined: 18 Sep 2012
Total Posts: 104
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 is not online. 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 is not online. 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 is not online. 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
SadisticNub is not online. SadisticNub
Joined: 05 Jan 2013
Total Posts: 4948
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
SadisticNub is not online. SadisticNub
Joined: 05 Jan 2013
Total Posts: 4948
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 is not online. 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
SadisticNub is not online. SadisticNub
Joined: 05 Jan 2013
Total Posts: 4948
24 Jul 2016 02:19 AM
BTW, there's a typo (print(someModule.someNunber) - it should be someNumber, not someNunber.
Report Abuse
sioux123 is not online. 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
SadisticNub is not online. SadisticNub
Joined: 05 Jan 2013
Total Posts: 4948
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 is not online. 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
SadisticNub is not online. SadisticNub
Joined: 05 Jan 2013
Total Posts: 4948
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image