Skip to content

Messages and colors

You've been sending plain messages so far, which works, but a bit of color makes them much easier to read. In this lesson you'll learn how to add color to a message and how to send one to everyone on the server, and you'll finish with an /announce command that broadcasts a colorful message.

Part 1: Add color to a message

Minecraft colors text with a special symbol, §, followed by a letter or number. For example §6 makes text gold and §c makes it red. That symbol is awkward to type, so most plugins let you write & instead and swap it afterwards. Put this helper at the very top of your main.lua, followed by a command that uses it:

main.lua
local function color(text)
    return (text:gsub("&", "\194\167"))
end

plugin.addCommand({
    name = "colors",
    description = "Show some colors",
    usage = "/colors"
}, function(event)
    local sender = event.getSender()
    sender:sendMessage(color("&6Gold &cRed &aGreen &9Blue &lBold &rNormal"))
end)

The color function takes some text and swaps every & for the § symbol. That swapping is what gsub does: text:gsub("a", "b") replaces every a in text with b. You'll use it again in the config lesson. The odd-looking \194\167 is just how Lua writes that symbol, so it works no matter how your file is saved. You don't need to understand how it works, just paste it at the top and use it anywhere. Then color("&6Gold ...") turns &6 into a real color code, and sender:sendMessage sends the result.

You can type the symbol yourself

If you'd rather skip the helper, typing § straight into your message works too, like sender:sendMessage("§6Hello"). Just make sure your file is saved as UTF-8, which VS Code does by default, or the symbol can come out garbled.

The codes work from left to right, so a color stays on until the next one changes it. &l makes text bold and &r resets everything back to normal. The table at the end of this page lists them all.

Try /colors and see which colors show up.

Part 2: Send a message to everyone

sender:sendMessage only reaches one person. To reach everyone on the server, ask the server to broadcast it:

main.lua
plugin.addCommand({
    name = "wave",
    description = "Wave at everyone",
    usage = "/wave"
}, function(event)
    local sender = event.getSender()
    plugin.getServer():broadcastMessage(color("&e" .. sender:getName() .. " &7waves at everyone!"))
end)

plugin.getServer() gives you the server, and :broadcastMessage(...) sends a message to every player. Here we build the message from pieces, joining them with the two dots you saw earlier: a yellow &e, the sender's name, then a gray &7 for the rest. This command needs the color helper from Part 1 to be in your main.lua too.

Try /wave with another player online, and see that both of you get the message.

Part 3: Join the words together

When someone types /announce Server restarts soon, each word lands in its own spot in the list of arguments. To send the whole thing as one message, you need to join them back together:

main.lua
plugin.addCommand({
    name = "announce",
    description = "Announce a message to everyone",
    usage = "/announce <message>",
    minArgs = 1
}, function(event)
    local args = event.getArgs()
    local message = table.concat(args, " ")
    plugin.getServer():broadcastMessage(color("&6[Announcement] &f" .. message))
end)

table.concat(args, " ") glues every word in the list together with a space between them, so Server, restarts and soon become Server restarts soon. We also set minArgs = 1 so that LuaCord complains if someone types /announce with nothing after it, like you saw in the Commands lesson.

Putting it together

Combined, your main.lua looks like this:

main.lua
local function color(text)
    return (text:gsub("&", "\194\167"))
end

plugin.addCommand({
    name = "announce",
    description = "Announce a message to everyone",
    usage = "/announce <message>",
    minArgs = 1
}, function(event)
    local args = event.getArgs()
    local message = table.concat(args, " ")
    plugin.getServer():broadcastMessage(color("&6[Announcement] &f" .. message))
end)

The color helper runs on the whole message, so whoever uses the command can add their own colors. Try /announce &cThe server restarts in 5 minutes! and the text will come out red.

Quick reference

These are the color codes. Write them with a & in front, and the color helper does the rest.

Code Color Code Color
&0 Black &8 Dark gray
&1 Dark blue &9 Blue
&2 Dark green &a Green
&3 Dark aqua &b Aqua
&4 Dark red &c Red
&5 Dark purple &d Light purple
&6 Gold &e Yellow
&7 Gray &f White

These are the formatting codes:

Code Effect
&l Bold
&o Italic
&n Underline
&m Strikethrough
&k Scrambled text
&r Reset everything back to normal

Next up is working with players.