Skip to content

How do I get started?

For now, I'm going to place light's ModuleScript in ReplicatedStorage, but you can place it anywhere. There are three ways to use light's functions, properties, etc:

local light = require(ReplicatedStorage.light).shared--(1)!

  1. This should be used when writing code for the client and the server, I.e., a ModuleScript.

local light = require(ReplicatedStorage.light).client--(1)!

  1. This should be used when writing code for the client. I.e., inside a LocalScript.

local light = require(ReplicatedStorage.light).server--(1)!

  1. This should be used for writing code for the server. I.e., inside a Script.

What is a Message?

A "Message" is an event which can be fired to and from the server, and connected with a single callback.

What is a Datatype?

A datatype is something that represents some set of possible values. When you're sending something across the network with light, you'll need to define what you want that thing to look like. Here's an example of one of light's number Datatypes:

local u8 = light.datatypes.u8

The prefix u indicates the number is an unsigned integer (whole numbers greater than zero.) The number to the right, 8, is the number of "bits"1 the value will take up across the network. A u8 can represent any value between 0 and 255

Unsigned Integers

The maximum value for an unsigned integer can be calculated with (256 ^ (bits / 8) ) - 1

How do I create Messages?

Light lets you define a Datatype for every Messae you create. Assuming you do, you'll get good autocomplete and will never need to make sure the type of your data is correct from inside a Message's callback. Light handles it all for you in an optimized way. The recommended way to create messages is with a simple ModuleScript:

ReplicatedStorage.abc_messages (ModuleScript)
local light = require(ReplicatedStorage.light).shared

local types = light.datatypes

local container = light.container({--(1)!
    ping = types.literal(nil),--(2)!
}, "abc")

light.begin_replication()--(3)!

return container
  1. light.container(messages) creates a "list" of messages from the table provided so the messages can be used.

  2. types.literal(nil) means that the message doesn't contain any data, because it is literally nil.

  3. light.begin_replication() Can be called at any time to allow messages to be sent on the client or server, but it's best to get it out of the way. Calling multiple times or from multiple files has no additional effects. If you call it later on, all of the messages you try to send will still get queued up.

Sending Messages (from Client to Server)

To send a message from the client to the server, use light.send(message, data)

StarterPlayerScripts.client (LocalScript)
1
2
3
4
5
6
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local light = require(ReplicatedStorage.light).client
local messages = require(ReplicatedStorage.messages)

light.send(messages.ping)

Listening For Messages (on the Server)

Now, we need somewhere to listen for this message. The way to listen for a message being sent is with light.connect(message, callback)

ServerScriptService.server (Script)
1
2
3
4
5
6
7
8
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local light = require(ReplicatedStorage.light).server
local messages = require(ReplicatedStorage.messages)

light.connect(messages.ping, function(player)
    print("pong!")
end)

Sending Messages (from Server to Client)

light.send(), we can modify our Script from before to respond to ping:

ServerScriptService.server (Script)
1
2
3
4
5
6
7
8
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local light = require(ReplicatedStorage.light).server
local messages = require(ReplicatedStorage.messages)

light.connect(messages.ping, function(player)
    light.send(messages.ping, player)
end)

Listening for Messages (on the Client)

Now let's change our LocalScript to listen to the server's response:

StarterPlayerScripts.client (LocalScript)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local light = require(ReplicatedStorage.light).client
local messages = require(ReplicatedStorage.messages)

light.connect(messages.ping, function()
    print("pong!")
end)

light.send(messages.ping)

  1. 8 bits is also known as a byte. Bytes are generally a measurement we use to talk about how much space something takes to represent inside a computer.

    local bytes = bits // 8 & local bits = 8 · bytes