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.
The template
Section titled “The template”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 tochat.sendServerMessageis the player, so only the player who typed the command sees the answer. Pass the player you got, notplayer.id: an ID can belong to someone else once that player leaves (why)./wherereads 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.onPlayerMessagesees every chat line before it goes out. This one only logs it to the server console, so the line is still broadcast as normal.
Select it
Section titled “Select it”Open server.ini beside nativemp-server.exe and set the gamemode line under [server]:
gamemode=script\gamemodes\my-mode.tswatch=truewatch=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.