Skip to main content

Networking

Cradle comes with a built-in networking system that allows you to easily create and manage remote events, remote functions and remote properties. This allows you effectively communicate between the server and the client. The networking system is built on top of Roblox's RemoteEvents and RemoteFunctions.

tip

The networking system uses the middleware system to allow you to easily add middleware to remote events and remote functions.

Check out the Middleware guide to learn more about the middleware system.

Remote Events

Remote events are useful for sending data that doesn't need to be returned to the client. This would be useful when a Module would need to send data to the server, such as a player's input or a player's request to spawn a vehicle. Remote events are also useful for sending data to the client, such as a Notification or a Radio message.

There are two types of remote events: Remote Events and Remote Unreliable Events. Remote events are reliable, meaning that they will always reach the destination. remote unreliable Event are unreliable, meaning that they may not always reach the destination. The performance of remote events is also important to consider. Remote events are not cheap to use, so it is important to use them wisely. It may be a better idea to use a remote unreliable Event instead of a remote event if the data is not important to the essential gameplay.

Creating a Remote Event

To create a remote event, you can use the Cradle.RemoteComm.RemoteEvent.new() function. This object is a wrapper around Roblox's RemoteEvent class with additional functionality. The RemoteEvent object has the following methods:

-- Server
RemoteEvent:FireClient(player, ...) -- Fires the remote event to one player
RemoteEvent:FireAllClients(...) -- Fires the remote event to all players
RemoteEvent:FireClientsList(playerList, ...) -- Fires the remote event to the players that are in the given list
RemoteEvent:FireAllClientsExcept(player, ...) -- Fires the remote event to all players except the given player
RemoteEvent:AddMiddleware(priority ,function(player, ...)) -- Adds a middleware function to the remote event
RemoteEvent:Connect(function(player, ...)) -- Connects a function to the remote event
-- Client
RemoteEvent:FireServer(...) -- Fires the remote event to the server
RemoteEvent:Connect(function(...)) -- Connects a function to the remote event
info

"..." represents the arguments that will be passed to the remote event.

Creatng an Remote Unreliable Event

To create an unreliable remote event, you can use the Cradle.RemoteComm.RemoteUnreliableEvent.new() function. This object is a wrapper around Roblox's RemoteEvent class with additional functionality. The RemoteUnreliableEvent object has the following methods:

-- Server
RemoteUnreliableEvent:FireClient(player, ...) -- Fires the remote event to one player
RemoteUnreliableEvent:FireAllClients(...) -- Fires the remote event to all players
RemoteUnreliableEvent:FireClientsList(playerList, ...) -- Fires the remote event to the players that are in the given list
RemoteUnreliableEvent:FireAllClientsExcept(player, ...) -- Fires the remote event to all players except the given player
RemoteUnreliableEvent:AddMiddleware(priority ,function(player, ...)) -- Adds a middleware function to the remote event
RemoteUnreliableEvent:Connect(function(player, ...)) -- Connects a function to the remote event
-- Client
RemoteUnreliableEvent:FireServer(...) -- Fires the remote event to the server
RemoteUnreliableEvent:Connect(function(...)) -- Connects a function to the remote event
info

"..." represents the arguments that will be passed to the remote event.

Remote Functions

Remote functions are useful for sending data that needs to be returned to the client. This would be useful when a Module would need to send data to the server, such as a player's request to spawn a vehicle and receive a boolean back to see if the player can spawn the vehicle.

Cradle Only

Remote Functions are designed only to be used within a Service. Attempting to use a remote property outside of a Service will result in it being unaccessible to the client.

Creating a Remote Function

To create a remote function, you can use the Cradle.RemoteComm.RemoteFunction.new() function. This object is a wrapper around Roblox's RemoteFunction class with additional functionality. The RemoteFunction object has the following methods:

-- Server
RemoteFunction:AddMiddleware(priority ,function(player, ...)) -- Adds a middleware function to the remote function
-- Client
RemoteFunction:InvokeServer(...) -- Invokes the remote function on the server
info

"..." represents the arguments that will be passed to the remote event.

Remote Properties

Remote properties are useful for storing data that needs to be synced between the server and the client. This would be useful when a Module would need to store data that needs to be synced between the server and the client, such as a player's current vehicle.

Cradle Only

Remote properties are designed only to be used within a Service. Attempting to use a remote property outside of a Service will result in it being unaccessible to the client.

-- Link Issue Here --

Creating a Remote Property

To create a remote property, you can use the Cradle.RemoteComm.RemoteProperty.new() function. This object is a wrapper around Roblox's Value Instance with additional functionality. The RemoteProperty object has the following methods:

-- Server
RemoteProperty:Set(value) -- Sets the value of the remote property
RemoteProperty:Get() -- Gets the value of the remote property
-- Client
RemoteProperty:Get() -- Gets the value of the remote property