Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท UI and Events

BindableEvents and ModuleScripts

Share code and talk between scripts on the same side with ModuleScripts, require, BindableEvents and BindableFunctions, and build your own Signal module.

As an experience grows you will have many scripts: one for the shop, one for the inventory, one for sounds. Two problems appear quickly. First, you will copy the same helper function into several scripts. Second, one script will need to tell another that something happened. Roblox solves the first with ModuleScripts and the second with BindableEvents.

Both tools work inside one side of the game: server to server, or client to client. They never cross the network. Crossing the network is the job of remote events, covered in Remote Events. Roblox code is shown read-only; the runnable examples are JavaScript stand-ins for the underlying ideas.

ModuleScripts and require

A ModuleScript is a script that does not run on its own. It returns exactly one value, usually a table of functions, and other scripts load it with require.

LUA
-- ModuleScript named MathUtil, inside ReplicatedStorage
local MathUtil = {}

function MathUtil.clamp(value, min, max)
	return math.max(min, math.min(max, value))
end

function MathUtil.lerp(a, b, t)
	return a + (b - a) * t
end

return MathUtil
LUA
-- Any Script or LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MathUtil = require(ReplicatedStorage:WaitForChild("MathUtil"))

print(MathUtil.clamp(150, 0, 100)) --> 100

Key facts about modules:

  • The module's code runs once, the first time something requires it. Every later require returns the same table. That makes modules perfect for shared state, such as a settings table.
  • A module in ReplicatedStorage can be required by both server and client scripts. A module in ServerStorage or ServerScriptService is invisible to clients, so secrets and server-only rules belong there.
  • Each side gets its own copy. Server and client each run the module once, separately; they do not share variables.
  • Two modules that require each other form a cycle and will error. Restructure so dependencies flow one way.

Where to put a ModuleScript

ReplicatedStorage Server and clients can both use it.
ServerStorage Server only. Clients never see it.
StarterPlayerScripts Client-only helpers.

Put anything that must stay secret out of ReplicatedStorage.

1 / 2

Modules that behave like classes

A very common pattern is an object-like module with a constructor, built using a metatable. You will learn metatables in the Luau chapters; here the shape matters.

LUA
local Counter = {}
Counter.__index = Counter

function Counter.new(start)
	return setmetatable({ value = start or 0 }, Counter)
end

function Counter:Increment(by)
	self.value += by or 1
	return self.value
end

return Counter

Each call to Counter.new() makes an independent object, while the methods are shared. The colon in Counter:Increment passes the object as self.

BindableEvents

A BindableEvent is an instance that lets one script Fire an event and other scripts listen to it, without either script knowing about the other. This is sometimes called a publish/subscribe design.

LUA
-- Create it in Studio (inside ServerStorage, named RoundEnded), or in code
local roundEnded = ServerStorage:WaitForChild("RoundEnded")

-- Listener (one script)
roundEnded.Event:Connect(function(winnerName)
	print("Winner:", winnerName)
end)

-- Sender (another script)
roundEnded:Fire("Ana")

The listener connects to .Event; the sender calls :Fire(...). Arguments are copied, so tables are cloned rather than shared. Metatables and functions inside a table do not survive the copy, a detail that surprises many learners.

A BindableFunction is the same idea with a return value: the caller uses :Invoke(...) and a single handler assigned to .OnInvoke returns an answer. Since they pause the caller, prefer events unless you truly need an immediate answer.

A module-based Signal

Many developers skip BindableEvents and write a small Signal module instead. It avoids copying arguments and is faster. The version below is a JavaScript stand-in for that exact design, plus a stand-in for require that runs a module only once.

JS Module cache like require (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The module code runs once, a and b are the same table, and each counter object keeps its own value.

JS Publish and subscribe: a shop and a sound system

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The shop never mentions sounds or inventories. You can add or remove listeners without touching it, which keeps big projects manageable.

Choosing the right tool

You need to... Use
Reuse functions or constants ModuleScript
Share state on one side ModuleScript returning a table
Tell another script on the same side something happened BindableEvent or a Signal module
Get an answer from another script on the same side A module function (best) or BindableFunction
Talk between server and client RemoteEvent / RemoteFunction

Common mistakes

  • Requiring a server-only module from a LocalScript. The client cannot see ServerStorage, so require fails.
  • Expecting a table in a module to be shared between server and client. It is not.
  • Firing a BindableEvent before the listener has connected. The event is lost, so connect first or use a module with stored state.
  • Forgetting return at the end of a module, which makes require error.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many times does a ModuleScript's top-level code run on one side of the game?

    Roblox caches the returned value, so later require calls return the same result without running the code again.
  2. 2. Where should a module with secret server rules be stored?

    Clients can read ReplicatedStorage, Workspace and StarterGui, but they cannot see the server-only containers.
  3. 3. Can a BindableEvent send a message from a server Script to a LocalScript?

    BindableEvents never cross the network. Server-client communication needs RemoteEvents or RemoteFunctions.
  4. 4. Which method does the sender call on a BindableEvent?

    The sender calls Fire. Listeners use Event:Connect. Invoke belongs to BindableFunction.

Summary

  • A ModuleScript returns one value and runs once per side; require gives everyone the same table.
  • Put shared code in ReplicatedStorage and server-only code in ServerStorage.
  • A BindableEvent lets scripts on the same side communicate without knowing each other; arguments are copied.
  • A custom Signal module is a faster, copy-free alternative.
  • Server and client communicate through remotes, not Bindables.

Next: Services and GetService