Skip to content

Your first gamemode

Every server comes with a starting point: script\gamemodes\template.ts. Copy it to a name of your own, say my-mode.ts, in the same folder.

script/gamemodes/my-mode.ts
import { chat, commands, players, server } from "nativemp";
server.on("playerConnected", ({ player }) => {
chat.sendServerMessage(`Welcome, ${player.name}! Type /help.`);
});
commands.register("help", (player) => {
chat.sendServerMessage("Commands: /help, /where", player);
});
commands.register("where", (player) => {
const [x, y, z] = players.getById(player.id)?.position ?? [0, 0, 0];
chat.sendServerMessage(`You are at ${x.toFixed(1)}, ${y.toFixed(1)}, ${z.toFixed(1)}`, player);
});
// Return false to keep a chat line from the default global broadcast, e.g. to send it only to nearby players.
chat.onPlayerMessage((player, text) => {
server.log(`${player.name}: ${text}`);
});

What it does:

  • server.on("playerConnected", ...) runs once a player has joined, and greets them in everyone’s chat.
  • commands.register("help", ...) adds /help. The second argument to chat.sendServerMessage is the player, so only the player who typed the command sees the answer. Pass the player you got, not player.id: an ID can belong to someone else once that player leaves (why).
  • /where reads the player’s last known position. Positions are empty until the server has seen the player in the world, hence the ?? [0, 0, 0].
  • chat.onPlayerMessage sees every chat line before it goes out. This one only logs it to the server console, so the line is still broadcast as normal.

Open server.ini beside nativemp-server.exe and set the gamemode line under [server]:

server.ini
gamemode=script\gamemodes\my-mode.ts
watch=true

watch=true reloads the gamemode every time you save a file in its folder, while players stay connected. Start the server, join it from the launcher and type /where.

Project structure explains the files around your gamemode, and Running a local server the options for starting it.