Blog Archive
We’re constantly working to make ROBLOX Studio the most feature-rich yet user-friendly game-development environment possible. This week, we’ve launched Module Scripts, which essentially allow you to create and save libraries of code, and share them between scripts. The benefits are myriad: your code can be broken into smaller pieces and shared (rather than replicated), your code is better organized, and you save time hunting and fixing bugs, to name a few. We’re hoping the use of Module Scripts becomes a common practice among our scripters — writing organized code is one of the best ways to get a game off the ground and keep it running smoothly.
Be more organized than ever
The chief benefit of Module Scripts is efficiency. If you’re a game developer, you might find yourself replicating functions that apply to multiple parts of your game. If there’s a bug in that code, you end up spreading it, which can turn into a major problem quickly. Module Scripts are similar to the current “BindableFunctions”, though they’re easier and more efficient to use.
Let’s say your game has three types of NPCs (non-player characters), but they all behave the same. A Module Script would allow you to apply a single piece of code to all three types of NPCs. There’s no replication.
Here’s a sample Module Script:
-- Workspace.ModuleScript (a ModuleScript) local MyModule = {} function MyModule.WalkToPlayer() print("Walking to player") -- ... end function MyModule.AttackPlayer() print("Attacking player!") -- ... end return MyModule
Now, here’s an example of a script utilizing the Module Script:
-- Workspace.PetScript (a Script) local fluffinessRating = 34 local helperModule = require(Workspace.ModuleScript) helperModule.WalkToPlayer()
And another example of a script utilizing the Module Script:
-- Workspace.OgreScript (a Script) local smashAndBash = true local helperModule = require(Workspace.ModuleScript) helperModule.WalkToPlayer() helperModule.AttackPlayer()
Here are some general rules about Module Scripts:
- ModuleScripts do not execute on their own, unlike other kinds of scripts
- ModuleScripts source can be executed by using the new global function “require()”
- “Require()” takes one argument — a reference to a ModuleScript Instance
- ModuleScripts are like large functions — each ModuleScript should have a return statement as a Lua value returning from the call to Require()
- ModuleScripts can be require()ed regardless of where it is in the instance tree. They can also be require()ed even if they’ve never been in the DataModel
- ModuleScripts will run at most one time — if two scripts require() the same ModuleScript Instance, that script will run once, and produce the same exact return value to both require()ers
- ModuleScripts are allowed to yield, and when they do, any script currently require()ing will yield as well
We hope this features makes developing games a faster and easier experience. Keep an eye on the blog in the coming weeks — there are more exciting ROBLOX Studio features coming soon. To discuss this new feature with other developers, the Scripting Helpers forum is a good place to start.