Skip to content

Your first plugin

In this lesson you'll make a plugin that prints a message when it starts, adds a /hello command, and welcomes players when they join. We'll build it one piece at a time, then put it all together at the end.

1. Make the folder

In your server's plugins folder, create a folder named exactly MyPlugin.lkt. The .lkt ending is how LuaCord knows the folder is a plugin.

2. Add plugin.yml

Inside that folder, create a file called plugin.yml, which tells LuaCord about your plugin:

plugin.yml
name: MyPlugin
version: "1.0"
main: main.lua
author: YourName
description: My first LuaCord plugin

The name and version are what they sound like, main is the Lua file that runs first, and author and description are there for anyone reading about your plugin.

3. Write main.lua

Now create main.lua in the same folder. This is where your plugin's code goes, and we'll write it in three parts.

Part 1: Run something when the plugin starts

Start your main.lua with this:

main.lua
plugin.onEnable(function()
    logger.info("My plugin is enabled!")
end)

plugin is just plugin related stuff. plugin.onEnable lets you say "run this when my plugin starts". You hand it a function, and LuaCord runs that function once, when the server turns your plugin on. Inside it, logger.info prints a message to the console, which is an easy way to see that your plugin is alive. Later on, this is also a good place to set things up before players arrive.

Part 2: Add a command

Now add this below it:

main.lua
plugin.addCommand({
    name = "hello",
    description = "Say hello!",
    usage = "/hello"
}, function(event)
    local sender = event.getSender()
    sender:sendMessage("Hello from Lua!")
end)

plugin.addCommand takes two things. The first is a table that describes your command: name is what players type after the slash, description says what it does, and usage is how to use it. The second is a function that runs every time someone uses the command.

That function receives an event, which holds the details of who ran the command. You can name it anything you like, event is just a common choice.

Inside the function, the first line, local sender = event.getSender(), asks the event who ran the command and saves the answer as sender. The second line, sender:sendMessage("Hello from Lua!"), sends that person a message, which is why they see "Hello from Lua!" in chat.

Give every command a description

LuaCord only registers commands that have a description, so without one the command silently won't exist.

Dot or colon?

Things that come from Minecraft, like sender, are called with a colon, as in sender:sendMessage(...). If you use a dot on them you'll get an error like bad argument: userdata expected.

Part 3: Welcome players when they join

Finally, add this at the bottom:

main.lua
plugin.registerEvent("PlayerJoinEvent", function(event)
    local player = event:getPlayer()
    player:sendMessage("Welcome to the server!")
end)

An event is something that happens in the game, and plugin.registerEvent lets you react to it. The first part is the name of the event, here PlayerJoinEvent, which happens whenever a player joins. The function runs every time it does.

This time the event comes from Minecraft and describes what just happened. event:getPlayer() gives you the player who joined, and player:sendMessage(...) greets them. Because both come from Minecraft, they use a colon.

Putting it together

Combined, your main.lua looks like this:

main.lua
plugin.onEnable(function()
    logger.info("My plugin is enabled!")
end)

plugin.addCommand({
    name = "hello",
    description = "Say hello!",
    usage = "/hello"
}, function(event)
    local sender = event.getSender()
    sender:sendMessage("Hello from Lua!")
end)

plugin.registerEvent("PlayerJoinEvent", function(event)
    local player = event:getPlayer()
    player:sendMessage("Welcome to the server!")
end)

And your folder should look like this:

plugins/
└── MyPlugin.lkt/
    ├── main.lua
    └── plugin.yml

4. Try it

Restart the server and you should see "My plugin is enabled!" in the console. Join the server and you'll get "Welcome to the server!", and typing /hello in game replies with "Hello from Lua!".

Changed your code?

On a folder plugin you can run /luacord dev reload myplugin to reload it without restarting the server.

When you want to share the plugin as a single file, the next step is to pack it as a .lkt.