Tutorial: a game mode
This tutorial builds Blade League, a complete game mode: knives only, instant respawns, first to a number of kills wins the match. Along the way it uses most of what a real mod needs: a mode, settings, events, the HUD, effects, saved data, languages and tests. Read Getting started first.
1. The plugin
./cs_plugin new blades
Edit blades/plugin.toml:
name = "blades"
version = "1.0.0"
author = "you"
description = "Knives only; the first to the kill goal wins."
api = 1
Empty main.luau; the steps below fill it from top to bottom.
2. The mode
A mode turns off the parts of the game it replaces and turns them back on when it stops. Blade League needs no buying, no bomb or hostages, and respawns players a second after they die:
local goal = cs.cvar("blades_goal", "15", { help = "Kills that win the match", min = 1, max = 100 })
cs.register_mode("blades", {
title = "Blade League",
off = { "buying", "item_drops" },
respawn = 1,
objectives = false,
start = function()
cs.chat(nil, "{green}Blade League{default}: first to " .. goal:int() .. " kills wins.")
end,
})
cs.on("plugin_start", function()
cs.set_mode("blades")
end)
cs.game.mode tells which mode runs, so every handler below starts with if cs.game.mode ~= "blades" then return end: the plugin stays quiet when an admin switches to another mode.
3. Knives only
The game gives each spawning player a knife and a pistol (player_equip). Block that, give a knife, and refuse anything picked up from the floor:
local function on(): boolean
return cs.game.mode == "blades"
end
cs.on("player_equip", function(ev)
if not on() then return end
ev:block()
ev.player:give("weapon_knife")
end, { before = true })
cs.on("player_pickup", function(ev)
if on() and ev.item.classname ~= "weapon_knife" then
ev:block()
end
end, { before = true })
before = true runs a handler before the game acts, which is what lets it block the game's own handling.
4. Counting kills
Each player's kills live in player:data(), a table the plugin keeps for that player until they leave or the map changes:
local function kills(p: Player): number
return p:data().kills or 0
end
local function leaders(): { Player }
local list = {}
for _, p in cs.players() do
if p.team == "t" or p.team == "ct" then
table.insert(list, p)
end
end
table.sort(list, function(a, b) return kills(a) > kills(b) end)
return list
end
5. The scoreboard on the HUD
A HUD widget stays on screen until removed; showing one with the same id again changes it in place:
local function show_board()
local items = {}
for i, p in leaders() do
if i > 5 then break end
table.insert(items, string.format("%d %s", kills(p), p.name))
end
cs.hud_widget(nil, "blades_board", {
kind = "list", title = "Goal " .. goal:int(), items = items,
x = 0.985, y = 0.25, align = "right", size = 20,
})
end
6. Kills, effects and the win
local over = false
local function win(p: Player)
over = true
cs.chat(nil, "{green}" .. p.name .. "{default} wins the match!")
cs.notify(nil, p.name .. " wins the match!", { title = "Blade League", kind = "success", size = "large", duration = 8 })
for _, other in cs.players() do
other:freeze(true)
end
cs.after(8, function()
cs.game:restart(1)
end)
end
cs.on("player_killed", function(ev)
if not on() or over then return end
local killer = ev.attacker and ev.attacker.player
if not killer or killer == ev.player then return end
local d = killer:data()
d.kills = (d.kills or 0) + 1
cs.particles(nil, ev.player.origin, { palette = 247, radius = 40 })
killer:play_sound("buttons/bell1.wav")
show_board()
if d.kills >= goal:int() then
win(killer)
end
end)
cs.on("game_restart", function()
over = false
for _, p in cs.players() do
p:data().kills = 0
end
show_board()
end)
cs.on("player_join", function() show_board() end)
cs.on("player_leave", function() cs.after(0, show_board) end)
Players frozen by win move again when they spawn after the restart.
7. Saving wins for good
player:data() forgets everything at the next map. Wins should last, so they go to a store keyed by the player's permanent ID:
local wins = cs.store("wins")
-- in win(p), before the notice:
-- wins:set(p.id, (wins:get(p.id) or 0) + 1)
cs.command("wins", function(player)
if player then
cs.reply(player, "You have won " .. (wins:get(player.id) or 0) .. " matches")
end
end, { help = "wins: your Blade League wins" })
The store answers reads from memory at once and writes to its file in the background.
8. Other languages
Move the texts into lang/en.toml:
start = "First to {goal} kills wins."
wins = "You have won {count} matches"
and lang/es.toml:
start = "Gana el primero en llegar a {goal} muertes."
wins = "Ganaste {count} partidas"
(Colour tags stay in the code: in a text, {name} is a value to fill in.) Then each player reads their own language:
for _, p in cs.players() do
cs.chat(p, "{green}Blade League{default}: " .. p:t("start", { goal = goal:int() }))
end
cs.reply(player, player:t("wins", { count = wins:get(player.id) or 0 }))
9. Tests
tests/main.luau:
local test = require("test")
test.case("players get only a knife", function(t)
local alice = t:client("Alice", { team = "t" })
t:wait_until(function() return alice.player.alive end)
t:equal(alice.player.weapon, "weapon_knife")
t:equal(#alice.player:weapons(), 1)
end)
test.case("the goal wins the match", function(t)
t:command("blades_goal 1")
local alice = t:client("Alice", { team = "t" })
local bot = t:bots(1, "ct")[1]
t:wait_until(function() return alice.player.alive and bot.alive end)
bot.entity:damage(500, { attacker = alice.player.entity })
alice:expect_text("wins the match!")
end, { timeout = 60 })
./cs_plugin test blades
10. Ship it
./cs_plugin check blades
./cs_plugin pack blades
Server admins install blades-1.0.0.csplug with cs_plugin install, and the server list shows "Blade League" in the Mode column.
Where to go from here
-
Custom weapons:
cs.register_weapon(damage, rate of fire, models, price) — see Reference mods for the gun game mod. - A HUD of your own with client scripts.
- Movement of your own (double jumps, dashes) that the player's game predicts: client scripts' shared movement.
-
Read the reference mods in
data/plugins/(deathmatch, zombie, jailbreak, surf, hide and seek, deathrun, gun game, races): each is a complete, working mode.