Home / Events

on()

on(eventName, handler)

Subscribes a function to a game event. You can add several handlers to the same event; they run in the order you registered them.

Parameters

NameTypeDescription
eventNamestringCore events fired by this build: ready, disable, pluginLoaded, pluginUnloaded, login, join, quit, chat, command, break, interact, place and move. Plugins may also listen for custom events sent with emit().
handlerfunctionCalled as function(e). Cancellable events may be cancelled with e:cancel([reason]).

Example

on('join', function(e)
  e.player:send('Welcome, ' .. e.player.name .. '!')
end)

on('chat', function(e)
  if e.message == 'spam' then
    e.player:send('No spam.')
    e:cancel()
  end
end)

Format chat with colors

Use color.* or color.translate() inside the chat event. This changes the message body while leaving the default player-name prefix unchanged.

on('chat', function(e)
  e.message = color.green .. e.message
end)

For a custom name and full-line format, translate a plugin-generated prefix, cancel the original chat, and send a replacement with server.broadcast(). See Chat colors.

Notes

Callbacks run synchronously on the server thread; keep them short. In this build, only login, chat, command, break, interact, place, and move use cancellation to affect the operation. join and quit are informational. The core event dispatcher does not stop later listeners after cancellation.