Version: 2.0.0
Basics
Quick Start#
Three files are needed to get started:
- roblox-ts
- luau
shared/remotes.ts
import Net from "@rbxts/net";
const Remotes = Net.Definitions.Create({
PrintMessage: Net.Definitions.Event<[message: string, other: string]>(),
MakeHello: Net.Definitions.AsyncFunction<(message: string) => string>(),
});
export { Remotes };
server/test.server.ts
import { Remotes } from "shared/remotes";
// listen to messages
const PrintMessage = Remotes.Server.Create("PrintMessage");
PrintMessage.Connect((player, message, other) => {
print(`Server recieved message: ${message} from player: ${player} ${other}`);
});
// listen and respond to messages
const MakeHello = Remotes.Server.Create("MakeHello");
MakeHello.SetCallback((player, message) => {
print(`Server got an async message from ${player} containing the message ${message}`);
return `Hello, ${player}! We got your message: ${message}`;
});
client/test.client.ts
import { Remotes } from "shared/remotes";
// send a message to the server
const PrintMessage = Remotes.Client.Get("PrintMessage");
PrintMessage.SendToServer("Hello there!", "other");
// send a message to the server, while listening for a response
const MakeHello = Remotes.Client.Get("MakeHello");
MakeHello.CallServerAsync("Net is cool right??").then((result) => {
print(`Client got a response to the async message from server: ${result}`);
});
src/shared/Remotes.lua
local Net = require(ReplicatedStorage.Net)
local Remotes = Net.Definitions.Create({
PrintMessage = Net.Definitions.Event(),
MakeHello = Net.Definitions.AsyncFunction(),
})
return Remotes
src/server/test.server.lua
local Remotes = require(ReplicatedStorage.Common.Remotes)
-- listen to messages
local PrintMessage = Remotes:CreateServer("PrintMessage")
PrintMessage:Connect(function (player, message, other)
print("Server recieved message", message, "from player:", player, other)
end)
-- listen and respond to messages
local MakeHello = Remotes:CreateServer("MakeHello")
MakeHello:SetCallback(function (player, message)
print("Server got an async message from ", player, "containing the message", message)
return "Hello, " .. tostring(player) .. " We got your message: " .. message
end)
src/client/test.client.lua
local Remotes = require(ReplicatedStorage.Common.Remotes)
-- send a message to the server
local PrintMessage = Remotes:GetClient("PrintMessage");
PrintMessage:SendToServer("Hello there!", "other");
-- send a message to the server, while listening for a response
local MakeHello = Remotes:GetClient("MakeHello");
MakeHello:CallServerAsync("Net is cool right??"):andThen(function (result)
print("Client got a response to the async message from server: ", result)
end)
And away you go - just get editing the remotes file to add your own definitions!