Functs
Functs are BufNet's version of a RemoteFunction, allowing the client to request data or an operation from the server and receive a response.
The client calls invoke(...) and yields until the server's handler returns.
Defining a Funct
A funct is created by calling funct:
return define("game", {
get_balance = funct({
value = t.nothing,
ret = t.u32,
}),
})In this example, get_balance does not require any input and returns an unsigned 32-bit integer.
value
Defines the data sent from the client to the server.
value = t.u8The client can then pass a value when invoking the funct:
net_client.my_funct.invoke(5)Use t.tuple(...) when the funct needs multiple input values:
value = t.tuple(t.string, t.u8)If the funct does not require any input, use t.nothing.
ret
Defines the data returned by the server.
ret = t.u32The value returned by the server is received by the client:
const balance = net_client.get_balance.invoke()
print(balance)Use t.tuple(...) when the server needs to return multiple values:
ret = t.tuple(
t.u32,
t.string
)Usage
Invoking (client)
-- client.luau
const balance = net_client.get_balance.invoke()
print(`Balance: {balance}`)The call yields until the server returns a value.
Handling (server)
-- server.luau
net_server.get_balance.on(function(player)
return player:GetAttribute("Balance") or 0
end)The returned value is sent back to the client that invoked the funct.
Passing Args
Functs can receive typed data from the client:
return define("game", {
get_item_price = funct({
value = t.string,
ret = t.u32,
}),
})The client provides the item name:
const price = net_client.get_item_price.invoke("sword")
print(`Price: {price}`)The server handles the request:
net_server.get_item_price.on(function(player, item_name)
const item = items[item_name]
if not item then
return 0
end
return item.price
end)This makes functs useful for request-and-response operations where the client needs a result from the server.