Bridge | Module to bridge the gap between server & client

I made this module as a library for a game framework I’m working on and realized it might be useful to other people. It’s fairly simple to use.

-- Server
local Bridge = require(game.ReplicatedStorage.Bridge)

local myBridge = Bridge.new('DoStuff', Bridge.Types.OPPOSITE)

game.Players.PlayerAdded:Connect(function(plr)
  myBridge:Trigger(plr) -- Going from server -> client so you have to provide the client
end)

-- Client
local Bridge = require(game.ReplicatedStorage.Bridge)

local myBridge = Bridge.getBridge('DoStuff')
myBridge:Connect(function()
  print('Bridge triggered!')
end)

You can also go from server → server or client → client using Bridge.

-- Server
local Bridge = require(game.ReplicatedStorage.Bridge)

local myBridge = Bridge.new('Server2Server', Bridge.Types.SAME)

wait(5)
myBridge:Trigger('Hello world!')

-- Server (another script)
local Bridge = require(game.ReplicatedStorage.Bridge)

game:GetService('RunService').Heartbeat:Wait() -- Ensure bridge has been created & registered

Bridge.getBridge('Server2Server'):Connect(function(txt)
  print(txt)
end)

You can download it here Bridge.lua.

EDIT: Check out my game framework Volt that incorporates a variation of this module that is much cleaner with a larger feature-set.

5 Likes

I’ve updated the module to fix a redundancy pointed out by @ToldFable as well as a few other optimizations. You can download the updated one here Bridge.lua.

You no longer need to use .register() and instead upon the creation of a bridge it will automatically register. I also added an alias for .getBridge().get()

In the main post I also forgot to mention all bridges must be established server-side.

3 Likes

This module is useless, it just provides an unnecessary layer of abstraction over remotes, I would expect at least some useful functionality from a module like this. A good example of frameworks that seemingly bridge the gap between server and client are Knit and Aero game framework (and a few more) as well as providing useful functionality.

I would expect at least some of that behaviour (since not everyone uses these framework) from this module. For example, I would expect a method on the server and then have the client call that method with no issues from your module, your module should be the one to handle these cases as well as the edge cases that may arise.

-- Server
local Bridge = require(game.ReplicatedStorage.Bridge)

local myBridge = Bridge.new('DoStuff', Bridge.Types.OPPOSITE)

function myBridge:GiveMeCash(player) 
    -- give the player the cash
end 

game.Players.PlayerAdded:Connect(function(plr)
    myBridge:GiveMeCash(plr)
end)

-- Client
local Bridge = require(game.ReplicatedStorage.Bridge)

local myBridge = Bridge.getBridge('DoStuff')

myBridge:GiveMeCash()
4 Likes

While I do see your point the exact example provided can be easily replicated with the current version of the module. As I had stated the original intent of creating this module was to build a game framework off of it. This extra layer of abstraction simplifies having to use remotes. The example you provided already works with the current version of my framework and is something that is layered on top of the module itself, however, I’ll take your criticism and build it directly into the module itself since it is meant to be standalone. Thanks for the feedback.

Under the hood RemoteFunctions & BindableFunctions are created using this module. The purpose is to simplify the creation and usage of them by creating a bridge between the client & server. I’ll probably work a bit more on this and increase the feature set to make it more appealing.

2 Likes