English
Sign in

Concepts

What every plugin author should know about how plugins run. The design notes go deeper; the API reference has every name.

A plugin

A plugin is a folder with a manifest (plugin.toml) and Luau files:

myplugin/
  plugin.toml          name, version, author, api, depends, permissions, hosts
  main.luau            run when the plugin loads
  lib/util.luau        require("./lib/util")
  lang/en.toml         its texts by language (player:t)
  content/             files players download: models, sounds, sprites, client scripts
  tests/main.luau      its tests (cs_plugin test)

Loading a plugin runs main.luau once, from top to bottom. That run registers what the plugin does: event handlers, commands, settings (cvars), timers, entity classes, weapons, modes. After that, the plugin only runs when something calls it: an event, a command, a timer, a callback.

Unloading (or plugin reload) removes everything it registered and closes its VM. Nothing stale survives a reload. To keep state across a reload, save it in a plugin_end handler (to a cs.store) and read it back when the plugin loads.

Isolation and safety

One thread, and work that waits

Every handler runs on the server's game thread, inside the server frame, one at a time. You never need locks. The flip side: a handler must return quickly, because the whole server waits for it.

Slow work runs on background I/O threads and comes back as a callback in a later frame: cs.http, Database:query, cs.read_file_async, WebSocket and socket events. The callback runs on the game thread like any handler. Database:query_now and cs.read_file block the frame: use them for small things at load time only.

Events

cs.on(name, handler, options) joins a handler to an event. The handler gets one event object with the event's fields (ev.player, ev.damage, ...). The reference lists every event and field.

-- Headshots only: other hits do nothing.
cs.on("player_trace_attack", function(ev)
	if ev.hit_group ~= 1 then -- 1: the head
		ev:block()
	end
end, { before = true })

An event nobody listens to costs nothing: the game checks a counter before building it. A server without plugins does not even start the plugin host.

Players and entities

A Player stands for a slot while its player is connected. Using a Player after that player left raises an error ("the player has left"); check player.connected when you keep one around (in a timer, for example). The same holds for Entity once its entity is removed (entity.valid).

Functions that show something take to: one Player, a list of them, or nil for everyone (cs.chat(nil, "...")).

Time

Settings and commands

Data

All in the plugin's data folder, never anywhere else:

cs.store(name)Key-value storage (JSON values, optional expiry). Reads are instant, writes happen in the background. The simplest choice.
cs.database(name)SQLite: real queries, indexes and transactions; :query runs in the background.
cs.read_json, cs.write_json, cs.read_toml, cs.write_tomlSettings files and small documents.
cs.read_file, cs.write_file, cs.append_file, cs.list_filesPlain files.
cs.read_buffer, cs.write_bufferBinary data (recordings).

Plugins working together

Languages

Put your texts in lang/en.toml, lang/es.toml, ... and show them with player:t("key", { name = ... }): each player gets the text in the language of their game (falling back to the plugin's own language).

What players see

cs.chat, cs.center, cs.consoleText in the chat, the centre of the screen, the console.
cs.notifyA styled notice box: title, icon, colours, progress bar, sound, at nine places.
cs.hud_widgetText, bars, icons, lists, timers and a radar on the HUD, kept until removed.
cs.menuA numbered menu (keys 1 to 9).
player:show_motdA web page or text window.
Effectscs.beam, cs.explosion, cs.sparks, cs.smoke, cs.sprite, cs.tracer, cs.decal, cs.blood, cs.particles, cs.dynamic_light, player:fade, player:shake.
Soundcs.emit_sound (in the world), player:play_sound, player:play_music.
World markscs.mark_line, cs.mark_text: lines and text drawn in the world.

Anything more needs a client script: Luau that runs in the players' games, sent by the server, for a custom HUD, effects and movement the client predicts.

Content

Files in the plugin's content/ folder (models, sounds, sprites, maps) are downloaded by players who lack them. Call cs.precache(path) in a map_start handler for each model, sprite or sound you use, and add a licence (content/LICENSE) for files you did not make.

Performance

Testing

cs_plugin test runs your tests/ folder against a real headless server with scripted players and bots. Write a case for each behaviour you care about; it is the cheapest way to keep a plugin working across game updates. See Tools.