My PubSub for Roblox module enables 4 way code communication via an Event Management system.
(Server/Server, Server/Client, Client/Client, Client/Server)
PubSub works with FilteringEnabled and allows for easy Publish and Subscribe functionality.
No Copylock Sample on Roblox: Publish/Subscribe Demo Place - Roblox
Why is this cool? Well, let’s say I wanted to announce a player has joined the game in chat.
The Player joining is something we connect to on the server [Players.PlayerAdded]
The chat is on the client, so the server and client need a RemoteEvent to communicate through.
You COULD create the RemoteEvent as a static object in ReplicatedStorage, then write code on both the server and the client to access that event and handle it as needed.
Of course, you have to hope the event is there when both the client and the server are looking for it.
You also have to do this for every RemoteEvent in your game…
ugh
Better way? PubSub.
PubSub creates RemoteEvents when they are needed and cleans them up when they aren’t.
PubSub ensures RemoteEvents are always available prior to attempted use.
PubSub also supports client to client and server to server eventing. RemoteEvents (alone) do not.
Check it out on GitHub: GitHub - HuotChu/roblox-pubsub: PubSub Module for Roblox Lua
Example:
ServerScriptService > Script
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local PubSub = require(ReplicatedStorage:WaitForChild('PubSub'))
local OnPlayerConnected = function (player)
wait(1)
PubSub.publish('AnnouncePlayer', player.Name)
end
Players.PlayerAdded:connect(OnPlayerConnected)
StarterGui > LocalScript
local StarterGui = game:GetService('StarterGui')
local PubSub = require(ReplicatedStorage:WaitForChild('PubSub'))
PubSub.subscribe('AnnouncePlayer', function (playerName)
StarterGui:SetCore('ChatMakeSystemMessage', {
Text = playerName..' joined the game!'
})
end, 'ChatMain')
- Note: I am currently working on v 2.0 of this which includes an authorization module to protect Remote Events/Functions