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

EventModule handlerWhen it runsArguments
tickonTickEvery game tick, 20 times a second, while you are in a worldnone
renderonRenderEvery framenone
chatonChatA chat message arrivesthe message, the sender’s name
attackonAttackYou hit somethingthe name of what you hit
keyonKeyA key is pressed or releasedthe key code, true when pressed
playerJoinonPlayerJoinSomeone joined the servertheir name
playerLeaveonPlayerLeaveSomeone left the servertheir name
deathonDeathYou diedx, y, z of where
respawnonRespawnYou are alive againnone
healthonHealthYour health changedhealth now, health before
worldJoinonWorldJoinYou entered a world or serverits address
worldLeaveonWorldLeaveYou left itits address
dimensionChangeonDimensionChangeYou went to another dimensionthe dimension now, the one before
moduleToggleonModuleToggleA module was turned on or off, by anythingits name, true when on
titleonTitleThe server showed a titlethe text, the kind
onEnableThe module is turned onnone
onDisableThe module is turned offnone

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 playerJoin down 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 .help shows.
  • The function gets the words typed after the command as a list: .hi Steve Alex gives {"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.