Version: 3.0.9
Basics
Quick Start#
Three files are needed to get started:
- roblox-ts
- luau
shared/remotes.ts
import Net from "@rbxts/net";
const Remotes = Net.CreateDefinitions({
PrintMessage: Net.Definitions.ClientToServerEvent<[message: string, other: string]>(),
MakeHello: Net.Definitions.ServerAsyncFunction<(message: string) => string>(),
});
export { Remotes };
server/test.server.ts
import { Remotes } from "shared/remotes";
// listen to messages
const PrintMessage = Remotes.Server.Get("PrintMessage");
PrintMessage.Connect((player, message, other) => {
print(`Server recieved message: ${message} from player: ${player} ${other}`);
});
// listen and respond to messages
const MakeHello = Remotes.Server.Get("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.CreateDefinitions({
PrintMessage = Net.Definitions.ClientToServerEvent(),
MakeHello = Net.Definitions.ServerAsyncFunction(),
})
return Remotes
src/server/test.server.lua
local Remotes = require(ReplicatedStorage.Common.Remotes)
-- listen to messages
local PrintMessage = Remotes.Server:Get("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.Server:Get("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.Client:Get("PrintMessage");
PrintMessage:SendToServer("Hello there!", "other");
-- send a message to the server, while listening for a response
local MakeHello = Remotes.Client:Get("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!