Skip to main content

server — Server API

Phase: Server

The server global provides access to server-level queries and actions.


Methods

server.broadcast(message)

Sends a plain-text message to every online player's chat.

server.broadcast("The server will restart in 5 minutes!")

server.getPlayers()table

Returns a sequential table of player context tables for every currently online player.

local players = server.getPlayers()
for _, p in ipairs(players) do
p.tell("Attention!")
end

server.getPlayer(name)player | nil

Returns the player context table for the player with the given name, or nil if they are not online.

local p = server.getPlayer("Notch")
if p then
p.tell("Hello, Notch!")
end

server.getPlayerCount()int

Returns the number of currently connected players.

logger.info("Online: " .. server.getPlayerCount())

server.runCommand(cmd)int

Executes a command string as the server (console) source. Returns the command's result code (0 on failure).

server.runCommand("say Hello everyone!")
server.runCommand("give Notch minecraft:diamond 64")

server.getDifficulty()string

Returns the current difficulty as a lowercase string: "peaceful", "easy", "normal", or "hard".

if server.getDifficulty() == "peaceful" then
server.broadcast("Peaceful mode is active.")
end

server.setDifficulty(name)

Sets the server difficulty. Accepts "peaceful", "easy", "normal", or "hard" (case-insensitive).

server.setDifficulty("hard")

server.setWeather(type)

Sets the overworld weather. Accepts "clear", "rain", or "thunder" (case-insensitive).

server.setWeather("thunder")

server.getMotd()string

Returns the server's MOTD (Message of the Day) string.


server.getTps()number

Returns the server's current TPS (ticks per second), capped at 20.0. Values below 20 indicate server lag.

local tps = server.getTps()
if tps < 15 then
logger.warn("Server TPS is low: " .. tps)
end