Back to Library
UI Nov 22, 2025

Chat Commands

T
by terrorsoul

Adds a /coords chat command that gives the player a subtle message with their coordinates.

LUA
-- Looks through all connected players and returns the playerId
local function GetPlayerIdByName(name)
    -- Loop through the list of current players
    for _, p in ipairs(tm.players.CurrentPlayers()) do
        -- Check if this player's name is the one we're looking for
        if p.playerName == name then
            -- Found the correct player, return their ID
            return p.playerId
        end
    end

    return nil
end

-- This function runs whenever any player sends a chat message
function OnChat(senderName, message, color)
    -- Check if the message is exactly "/coords"
    if message == "/coords" then
        -- Convert the sender's name into a usable playerId
        local playerId = GetPlayerIdByName(senderName)

        -- Get the player's transform
        local transform = tm.players.GetPlayerTransform(playerId)

        -- Read their world coordinates from the transform
        local pos = transform.GetPositionWorld()

        local msg = string.format("X=%.2f Y=%.2f Z=%.2f", pos.x, pos.y, pos.z)

        -- Show the coordinates as a subtle on-screen message
        tm.playerUI.AddSubtleMessageForPlayer(
            playerId,
            "Coordinates",
            msg,
            5,   -- duration in seconds
            ""   -- no icon
        )
    end
end

-- Register our chat listener so the game actually uses it
tm.playerUI.OnChatMessage.add(OnChat)

Discussion

Please log in to post a comment.