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 VM per plugin. Each plugin has its own Luau state, sandboxed: globals and libraries are read-only, there is no
ioand noos.execute, and a plugin cannot reach another's variables. Plugins talk to each other only through exports and events (below). -
Files are read and written only in the plugin's data folder (
plugin_data/<name>/).requireloads only the plugin's own files (and libraries of plugins it depends on,@other/...). -
Errors in a handler are caught and printed with the file, line and stack. They never reach the game. A plugin that errors 20 times in a minute is paused until
plugin resume. -
Time and memory budgets. One call into a plugin may take at most 100 ms (
sv_plugin_budget_ms) and a plugin may use at most 256 MB (sv_plugin_memory_mb). Past them the call is stopped with an error. -
Permissions in
plugin.tomlunlock the few dangerous things:raw_properties(any entity field by name),raw_messages(game messages written by hand),raw_sockets(TCP and UDP),server_console(cs.server_commandand setting the server's settings: the server console's full powers, development mode aside). Network access (cs.http,cs.websocket) reaches only the hosts the manifest lists.
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.
- After (default): the handler runs once the game handled the event, and sees the result.
-
Before (
{ before = true }): the handler runs first and may change the writable fields (ev.damage = ev.damage * 2) or, on blockable events, callev:block()to stop the game's own handling. -
Priority (
{ priority = 10 }, default 0) orders handlers across plugins: higher runs first. -
Filters: entity events take
{ class = "func_door" }or{ entity = e };client_messagetakes{ message = "TextMsg" }. - The event object lives only during the call; keep the values you need, not the object.
-
cs.onreturns aHook;hook:remove()takes the handler out.
-- 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).
-
cs.players()lists the players,cs.player(slot)finds one. -
player.idis the stable player ID (CSID:...): use it as the key for anything you keep about a player between games.BOTfor bots. -
player:data()is a table the plugin keeps for the player until it leaves or the map changes: the place for per-game state. -
player.entityis the player's body as anEntity(fields every entity has:origin,velocity,health, ...).
Functions that show something take to: one Player, a list of them, or nil for everyone (cs.chat(nil, "...")).
Time
-
cs.time()is the game's clock in seconds. -
cs.after(seconds, fn)runs once,cs.every(seconds, fn)repeats. Both return aTimer(timer:cancel()). With{ player = p }the timer ends by itself when that player leaves. -
The
frameevent runs every server frame: keep it very light, or use a timer.
Settings and commands
-
cs.cvar(name, default, options)registers a setting the server admin can change inserver.cfgor the console. Read it with:number(),:int(),:bool()or.value; thecvar_changeevent tells when it changes. Name it after your plugin (hello_message). -
cs.command(name, handler, options)adds a command.admin = trueallows only admins whoseadmins.tomlentry allows that command.player:allowed("name")checks any permission you invent, andplayer:can_target(other)respects admin immunity.
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_toml | Settings files and small documents. |
cs.read_file, cs.write_file, cs.append_file, cs.list_files | Plain files. |
cs.read_buffer, cs.write_buffer | Binary data (recordings). |
Plugins working together
-
cs.export(name, fn)offers a function;cs.import(plugin, name)gets another plugin's. List the other plugin independsso it loads first. -
cs.emit("event", ...)fires an event of your own; other plugins listen withcs.on("yourplugin:event", fn). -
require("@other/lib/x")loads a library file of a plugin you depend on.
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.console | Text in the chat, the centre of the screen, the console. |
cs.notify | A styled notice box: title, icon, colours, progress bar, sound, at nine places. |
cs.hud_widget | Text, bars, icons, lists, timers and a radar on the HUD, kept until removed. |
cs.menu | A numbered menu (keys 1 to 9). |
player:show_motd | A web page or text window. |
| Effects | cs.beam, cs.explosion, cs.sparks, cs.smoke, cs.sprite, cs.tracer, cs.decal, cs.blood, cs.particles, cs.dynamic_light, player:fade, player:shake. |
| Sound | cs.emit_sound (in the world), player:play_sound, player:play_music. |
| World marks | cs.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
-
The server runs 100 frames a second or more, and some events fire for every player every frame (
player_move,player_pre_think,frame). Do little there, and only when needed. -
Prefer events to polling, and timers to
frame. - Cache what you look up often (cvars, entities) instead of finding it again on every call.
-
plugin profshows what each handler costs, per frame.
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.