npm i @rbxts/net-functions
First make two objects with eventName: readonly t.check<any>[]
and eventName: t.check<any> | readonly t.check<any>[]
pairs, for example
import { t } from "@rbxts/t"
const GameFunctions = {
getData = [t.number, t.boolean] as const;
doThing = [] as const;
}
const GameFunctionsReturns = {
getData = t.string;
doThing = [t.number, t.string] as const;
}
Then create a server and client like so:
//server
import { NetFunctionsServer as Server } from "@rbxts/net-functions";
const server = new Server(GameFunctions, GameFunctionsReturns);
//client
import { NetFunctionsClient as Client } from "@rbxts/net-functions";
const client = new Client(GameFunctions, GameFunctionsReturns);
where GameFunctions is your event object, and GameFunctionsReturns contains event return checks.
To use multiple servers and clients add in an id parameter to the constructor:
import { NetFunctionsClient as Client } from "@rbxts/net-functions";
const client = new Client(GameFunctions, GameFunctionsReturns, "id1");
import { NetFunctionsServer as Server } from "@rbxts/net-functions";
import { t } from "@rbxts/t";
const events = {
getData: [t.number, t.boolean] as const,
};
const eventsReturns = {
getData: t.string,
};
const server = new Server(events, eventsReturns, "main");
server.on("getData", (player, number, boolean) => {
return `${number}: ${boolean}`;
});
import { NetFunctionsClient as Client } from "@rbxts/net-functions";
import { t } from "@rbxts/t";
const events = {
getData: [t.number, t.boolean] as const,
};
const eventsReturns = {
getData: t.string,
};
const client = new Client(events, eventsReturns, "main");
const data = client.invoke("getData", 0, true);
print(`Data: "${data}"`);