Your first resource
A /hola command and a key that asks the server for a gift, in Lua and JavaScript, started live.
We are going to make saludo (greeting): a /hola command that replies in the chat, and a key (H) that asks the server for a gift and shows it on screen. It uses the exports of sixm-base (chat and notices) and sixm-rp (money), which ship with SixM.
1. The folder
datos-servidor/recursos/saludo/
recurso.lua
servidor.lua
cliente.lua
2. The manifest
-- recurso.lua
return {
version = "1.0.0",
autor = "Yo",
descripcion = "Mi primer recurso",
depende = { "sixm-base", "sixm-rp" },
servidor = { "servidor.lua" },
cliente = { "cliente.lua" },
}
depende (depends) makes those resources start before this one.
3. The server
-- servidor.lua
local base = sixm.importa("sixm-base")
local rp = sixm.importa("sixm-rp")
local ya = {} -- player id → true, to give the gift only once
-- Without this, the server drops the event when a client sends it.
sixm.red("saludo:regalo")
sixm.comando("hola", function(id, args)
base.decir(id, "¡Hola, " .. base.nombre(id) .. "!")
end)
sixm.on("saludo:regalo", function(id)
if ya[id] then return base.avisar(id, "Ya te lo di", "error") end
if rp.dar_dinero(id, 100, "efectivo", "regalo de bienvenida") then
ya[id] = true
sixm.al_cliente(id, "saludo:hecho", 100)
end
end)
sixm.on("jugador:sale", function(id) ya[id] = nil end)
The handler of a network event receives the id first, the one of whoever sent it. Never trust what the client sends: the server decides.
4. The client
-- cliente.lua
sixm.tecla("H", function()
sixm.al_servidor("saludo:regalo")
end)
sixm.on("saludo:hecho", function(cantidad)
sixm.emitir("sixm:aviso", "Te regalan " .. cantidad .. " $", "dinero")
end)
sixm:aviso is the client event of sixm-base that draws a notice at the top right.
5. Start it
Without restarting the server, in the console (or in the panel's Recursos (Resources) tab):
arrancar saludo
Connected players receive it right away. Every time you change something: reiniciar saludo (restart). To make it always start, add this to servidor.cfg:
asegura saludo
Errors appear in the console with the resource's name.
The same in JavaScript
// recurso.json
{
"version": "1.0.0",
"depende": ["sixm-base", "sixm-rp"],
"servidor": ["servidor.js"],
"cliente": ["cliente.js"]
}
// servidor.js
const base = sixm.importa('sixm-base');
const rp = sixm.importa('sixm-rp');
const ya = new Set();
sixm.red('saludo:regalo');
sixm.comando('hola', (id) => base.decir(id, `¡Hola, ${base.nombre(id)}!`));
sixm.on('saludo:regalo', (id) => {
if (ya.has(id)) return base.avisar(id, 'Ya te lo di', 'error');
if (rp.dar_dinero(id, 100, 'efectivo', 'regalo de bienvenida')) {
ya.add(id);
sixm.al_cliente(id, 'saludo:hecho', 100);
}
});
sixm.on('jugador:sale', (id) => ya.delete(id));
// cliente.js
sixm.tecla('H', () => sixm.al_servidor('saludo:regalo'));
sixm.on('saludo:hecho', (cantidad) => sixm.emitir('sixm:aviso', `Te regalan ${cantidad} $`, 'dinero'));
sixm-rp is written in Lua and this resource in JS: sixm.importa does not care about the language. JS differences are covered in JavaScript.
Next
- Add an interface: HTML interfaces.
- Save data across restarts: Storage (KVP).
- Use the game: Natives.