Skip to content

ClientScripts and Channel

ClientScripts

server.clientScripts: JavaScript every player's game runs, in a sandbox. See Client scripting for the script side.

set(name, code) Adds or replaces a script, for everyone now and as they join.
setFile(path, name?) From a file on the server (sent to each player).
useClientFile(path) A client file: players already have it, only its name is sent.
delete(name) Removes it from everyone's game.
has(name), names

Scripts run in the order they were first added. Replacing or removing one runs them all again from scratch.

Channel

server.channel(name): a named channel to and from client scripts. Data is anything JSON can hold. Names are 1-64 characters; sagl: is reserved.

on((player, data) => ...) What scripts send (sagl.emit(name, data)).
off(handler)
send(player, data?) To one player's scripts.
broadcast(data?) To everyone's.

player.emit(channel, data) is the same as send. Every message also fires the clientMessage event. Messages are up to 256 KB.

const shop = server.channel("shop");
shop.on((player, { buy }) => {
  if (buy === "pistol" && player.money >= 200) {
    player.giveMoney(-200);
    player.giveWeapon(Weapon.Pistol, 50);
  }
});