@rbxts/simplesignals v1.3.1
SimpleSignals
A Roblox-TS RemoteEvent/RemoteFunction/BindableEvent wrapper which puts and end to the hassle of managing events. You can get straight to connecting and firing without any WaitForChild/Instance.new boilerplate, while your event instances get created automatically in the background.
Usage
Example: working with an event called "z"
SimpleClient.on("z", callbackFunc)
gets called on the client, but z
doesn't exist. SimpleSignals will WaitForChild("z")
on the event folder, and when the event gets created on the server, it connects callbackFunc
to it.
But how do events get created?
Any time you call a z
RemoteEvent related function on the server (on(z
, once(z
, fire(z
), a RemoteEvent with name z
gets created - if such a RemoteEvent doesn't exist. RemoteEvents/RemoteFunctions cannot be created on the client.
The same process follows for RemoteFunctions and BindableEvents (note that BindableEvents aren't parented anywhere).
You can import the module this way on the server:
import { Server as SimpleSignals } from "@rbxts/simplesignals";
and on the client:
import { Client as SimpleSignals } from "@rbxts/simplesignals";
SimpleSignals manages events cleanly, without you having to instantiate or to WaitForChild
:
SimpleClient.on("printX", (player, x) => {
print(x);
});
SimpleClient.fire("printX", "X");
You never have to create objects you only use once:
const printX = new Instance("RemoteEvent");
printX.Name = "printX";
printX.Parent = game.GetService("ReplicatedStorage");
printX.OnServerEvent.Connect((player, x) => {
print(x);
});
const printX = game.GetService("ReplicatedStorage").WaitForChild("printX");
printX.FireServer("X");
The following table describes where each event is stored:
Event type | Game location | Folder name | Path |
---|---|---|---|
RemoteEvent | ReplicatedStorage | RemoteEvents | game.ReplicatedStorage.RemoteEvents |
RemoteFunction | ReplicatedStorage | RemoteFunctions | game.ReplicatedStorage.RemoteFunctions |
BindableEvent | none* |
*BindableEvents aren't parented anywhere. They're stored in an internal table.
API
- Simple:setCallback(
name
: string,callback
: Function) →Promise<void>
- Simple:invoke(
name
: string,...args
) →Promise<T>
- Simple:registerFunction(
name
: string) →void
(only on the server)
- Simple:onBindable(
name
: string,callback
: Function) →RBXScriptConnection
- Simple:onceBindable(
name
: string,callback
: Function) →void
- Simple:fireBindable(
name
: string,...args
) →void
Examples
Redeem a code for a reward
main.server.ts
import { Server as simple } from "@rbxts/simplesignals";
const rewards = {
"ABC123": 500,
"1thousand": 1000
}
simple.on("redeemCode", (player, code: string) => {
const reward = rewards[code];
if (reward) {
print(`${player.Name} just got ${reward} Coins!`);
}
});
main.client.ts
import { Client as simple } from "@rbxts/simplesignals";
import { Players } from "@rbxts/services";
const LocalPlayer = Players.LocalPlayer;
const screenGui = new Instance("ScreenGui");
screenGui.Parent = LocalPlayer.WaitForChild("PlayerGui") as Folder;
const textBox = new Instance("TextBox");
textBox.Position = UDim2.fromScale(0.5, 0.5);
textBox.Parent = screenGui;
textBox.FocusLost.Connect(enterPressed => {
if (enterPressed) simple.fire("redeemCode", textBox.Text);
});