Scripting
Events and commands
Run code on ticks, frames, chat, hits, keys, joins, deaths and titles, and add chat commands.
There are two ways to react to the game:
- Module handlers, like
function mod:onTick(), run only while that module is on. See Script modules. - Script handlers, added with
lumen.on, run all the time while the script is loaded:
lumen.on("tick", function() end)
lumen.on("render", function() end)
lumen.on("chat", function(message, sender) end)
lumen.on("attack", function(name) end)
lumen.on("key", function(key, down) end)
A script can add as many handlers for the same event as it likes.
The events
| Event | Module handler | When it runs | Arguments |
|---|---|---|---|
tick | onTick | Every game tick, 20 times a second, while you are in a world | none |
render | onRender | Every frame | none |
chat | onChat | A chat message arrives | the message, the sender’s name |
attack | onAttack | You hit something | the name of what you hit |
key | onKey | A key is pressed or released | the key code, true when pressed |
playerJoin | onPlayerJoin | Someone joined the server | their name |
playerLeave | onPlayerLeave | Someone left the server | their name |
death | onDeath | You died | x, y, z of where |
respawn | onRespawn | You are alive again | none |
health | onHealth | Your health changed | health now, health before |
worldJoin | onWorldJoin | You entered a world or server | its address |
worldLeave | onWorldLeave | You left it | its address |
dimensionChange | onDimensionChange | You went to another dimension | the dimension now, the one before |
moduleToggle | onModuleToggle | A module was turned on or off, by anything | its name, true when on |
title | onTitle | The server showed a title | the text, the kind |
onEnable | The module is turned on | none | |
onDisable | The module is turned off | none |
tick
Runs at the start of your player’s tick. This is the place for game logic: checking your health, counting things, reacting to players. player.* values are fresh here.
render
Runs every frame, which can be outside a world too, so check player.inWorld() before you draw something that needs one. The draw functions only work inside a render handler. Keep it light: this runs a hundred or more times a second.
chat
Gets every chat message the server sends you, with formatting codes left in. sender is empty for messages that do not come from a player, like server announcements. Return true to hide the message.
lumen.on("chat", function(message, sender)
if message:lower():find("discord.gg") then return true end
end)
attack
Runs when you hit an entity, with its name (formatting removed). Return true to cancel the hit.
lumen.on("attack", function(name)
for _, p in ipairs(world.players()) do
if p.name == name and p.teammate then return true end
end
end)
key
Gets the Windows key code (65 is A, 32 is space, and so on) and whether the key went down or up. It also runs while you type in chat, so check client.screen() if that matters:
lumen.on("key", function(key, down)
if down and key == 0x47 and client.screen() == "hud_screen" then -- G
client.notify("G", "pressed")
end
end)
To check whether a key is held right now, use input.isKeyDown instead.
playerJoin and playerLeave
Run when a name is added to or dropped from the server’s player list.
lumen.on("playerJoin", function(name) client.notify("Joined", name) end)
death, respawn and health
health runs whenever your health changes, with the new value and the one before. death runs when it reaches zero, with the place you died, and respawn when you are alive again.
lumen.on("health", function(now, before)
if now < before and now <= 6 then client.notify("Low health", tostring(now)) end
end)
worldJoin, worldLeave and dimensionChange
worldJoin and worldLeave get the address world.serverAddress() gives. dimensionChange gets the dimension you are in now and the one you left, for example "Nether", "Overworld".
moduleToggle
Runs when any module is turned on or off, whether by you, a keybind, a command or a script. It gets the module’s name and true when it is now on. Toggles are noticed within about a tenth of a second.
title
Runs when the server shows a title, a subtitle or an action bar line. It gets the text, with formatting codes left in, and the kind: "title", "subtitle" or "actionbar". Return true to hide it.
lumen.on("title", function(text, kind)
if kind == "actionbar" then print(text) end
end)
Good to know
- For the first three seconds in a world nobody joins: the players already there when you arrive are not news. The same goes for the players there when the script loads.
- The events from
playerJoindown cost nothing until a script handles them. A handler added later starts to fire within a second.
Chat commands
lumen.command("hi", "Says hi.", function(args)
print("hi " .. (args[1] or ""))
end)
- The first argument is the command’s name, one word. Type it with your prefix, here
.hi. - The second is the description
.helpshows. - The function gets the words typed after the command as a list:
.hi Steve Alexgives{"Steve", "Alex"}. - A script cannot take the name of a built-in command. Two scripts adding the same name replace each other.
- A script’s commands go away when it is unloaded and come back when it loads.
To run a Lumen command from a script, use client.command:
client.command("toggle zoom")
client.command("config reload")
It returns true when the command is on its way, or false and the reason. Scripts are refused a few commands, listed in the API reference.