RemoteFunction
Lets a player’s device call the game server and wait for an answer.
- Inherits: Instance
- Creatable: Yes:
new RemoteFunction(), orInstance.new('RemoteFunction', parent). - Replication: Replicated: clients get a copy, and changes the server makes reach them.
A LocalScript calls await remote.invokeServer(...); the server answers with the function you
assign to onServerInvoke, which receives the calling player first. Arguments and the return
value cross the network like RemoteEvent arguments, with the same
limits, and count toward the same 60 calls a second.
The call rejects with an error when:
- the server’s handler throws (the error’s message is passed along);
- the server has no
onServerInvokefor it; - the handler takes longer than 5 seconds of game time;
- the player is over the rate limit;
- no answer arrives within 10 seconds, or the player disconnects.
There’s no server-to-player invoke: use a RemoteEvent, and another one for the reply.
Properties
Section titled “Properties”onServerInvoke
Section titled “onServerInvoke”Type: ((player: Player, ...args: unknown[]) => unknown) | null · Access: read/write
Server: set this to answer; it may be async. The first argument is the calling player. When the script that set it stops, it’s cleared (unless something else set it since).
A RemoteFunction has one handler: assigning replaces the previous one. Treat the arguments as untrusted, like RemoteEvent arguments.
Inherited from Instance: name, archivable, id, className, parent, destroyed
Methods
Section titled “Methods”invokeServer
Section titled “invokeServer”invokeServer(...args: unknown[]): Promise<unknown>Player’s device: calls the server’s onServerInvoke with these arguments and resolves with what
it returns. Only works on a player’s device.
Inherited from Instance: getPropertyChangedSignal, setAttribute, getAttribute, getAttributes, getAttributeChangedSignal, getChildren, getDescendants, findFirstChild, findFirstChildOfClass, findFirstChildWhichIsA, findFirstAncestor, findFirstAncestorWhichIsA, waitForChild, isA, isDescendantOf, isAncestorOf, getFullName, clearAllChildren, destroy, clone, toString
Events
Section titled “Events”Inherited from Instance: changed, childAdded, childRemoved, descendantAdded, descendantRemoving, ancestryChanged, destroying, attributeChanged
Examples
Section titled “Examples”A shop price list
Section titled “A shop price list”import { RemoteFunction, ReplicatedStorage, getChild } from '@rally/sdk';
// Scriptconst getPrices = getChild(ReplicatedStorage, 'GetPrices', RemoteFunction);const prices: Record<string, number> = { hat: 50, cape: 120 };getPrices.onServerInvoke = (player, item) => { if (typeof item !== 'string' || !(item in prices)) throw new Error('unknown item'); return prices[item];};import { RemoteFunction, ReplicatedStorage, print, task, waitForChild } from '@rally/sdk';
// LocalScripttask.spawn(async () => { const getPrices = await waitForChild(ReplicatedStorage, 'GetPrices', RemoteFunction); try { const price = await getPrices.invokeServer('hat'); print('a hat costs', price); } catch (e) { print('could not get the price:', e); }});