Concepts/Talking_between_server_and_client
Server and client scripts are separate programs; they call each other through two functions:
| Direction | Call | Rule |
|---|---|---|
Client -> server | CallServerFunc("Main/Server.nut", "functionName", args...) | The server must first allow it: RegisterRemoteFunc("functionName"). Anything not registered is dropped. The server function receives exactly the arguments the client sent. The caller is not added automatically, so pass something that identifies the player (a Player handle such as FindLocalPlayer(), or the player name) and look it up on the server. |
Server -> client | CallClientFunc(player, "Main/Client.nut", "functionName", args...) | Calls a global function in that player's client script. No registration required. The function must exist in a client script (not inside a function). |
Allowed argument types: integer, float, string (max 70 characters), bool, null, Player and Vehicle handles; CallClientFunc also accepts Vector. Up to 16 arguments. Other values (tables, arrays, other entities) are skipped. The script-name argument is kept for compatibility with older gamemodes; only the function name decides what is called.
CarefulTreat everything arriving through
RegisterRemoteFunc as untrusted input. A modified client can call any registered function with any arguments - including naming a different player than itself. Never let a remote function directly give cash, weapons or admin rights without checking state held on the server (for example "is this player currently in the shop?").// ---- Server.nut ----
RegisterRemoteFunc("buyHealth");
function buyHealth(player)
{
if (player.Cash >= 100) { player.Cash = player.Cash - 100; player.Health = 100.0; }
}
// ---- Client.nut ----
function onBuyPressed() { CallServerFunc("Main/Server.nut", "buyHealth", FindLocalPlayer()); }Categories: Core concepts