Sending Messages from Other Scripts

How to send chat messages from your custom scripts.

Basic Usage

In order to send a message we need two things: a template (how will the message look) and args (what content are we writting) in the template.

Generally you can use our template builder in order to easily create a template.

    local T = exports['chat']:getThemeBuilder()

    -- API Looks something like
    -- "consumes" Means that it'll pick up it's corresponding arg from the "args" field when
    -- triggering "chat:addMessage" like shown below.
    local template = T.build()
        :tag('TEST')          -- Consumes nothing
        :author()             -- ← consumes args[1]
        :text(': ')           -- Consumes nothing
        :message()            -- ← consumes args[2]
        :spacer()             -- Consumes nothing
        :text('Extra info')   -- Consumes nothing
        :done()

For more information regarding templates refer to:

Templates
../templates/

Using Templates

Getting the Theme Builder

Before building custom templates, get the theme builder export:

Creating runtime template - via export

local T = exports['chat']:getThemeBuilder()

TriggerEvent('chat:addMessage', {
    template = HTML.createTemplate(
        T.build()
            :bg('#1a1a2e')
            :tag('CUSTOM', '#00d4ff')
            :author({ bold = true })
            :text(': ')
            :message()
            :done()
    ),
    args = { GetPlayerName(PlayerId()), "Custom message" }
})

Registered Template - via Config templates

-- Use a template from Config.Theme
TriggerEvent('chat:addMessage', {
    templateId = 'cmd:me',
    args = { GetPlayerName(PlayerId()), "does something" }
})

Channels

Send to Channel

TriggerEvent('chat:addMessage', {
    templateId = 'cmd:me',
    channel = 'police',     -- Only police channel members see
    args = { GetPlayerName(PlayerId()), "radio message" }
})

Important Notes

Args Consumption

Not all components consume args:

Consumes args:

  • T.author() - uses next arg
  • T.message() - uses next arg

Does NOT consume args:

  • T.tag(text) - static
  • T.text(content) - static
  • T.icon(svg) - static
  • T.spacer() - static
  • T.bold(), T.italic() - static

Placeholder Processing

  • Client templates: placeholders replaced on client
  • Server templates (dont_register:): placeholders replaced on server
  • Framework values: requires framework to be loaded

Template ID vs Template

  • templateId - Use registered template from Config.Theme
  • template - Inline template (must use HTML.createTemplate())

Don't mix them:

-- ✓ Correct
TriggerEvent('chat:addMessage', {
    templateId = 'cmd:me',  -- Uses Config.Theme['cmd:me']
    args = { ... }
})

-- ✓ Correct
TriggerEvent('chat:addMessage', {
    template = HTML.createTemplate(T.build()...),  -- Inline
    args = { ... }
})

-- ✗ Wrong - don't use both
TriggerEvent('chat:addMessage', {
    templateId = 'cmd:me',
    template = HTML.createTemplate(...),  -- Conflict!
    args = { ... }
})

Backward Compatibility

Still works (legacy format):

TriggerEvent('chatMessage', 'PlayerName', 'msg', message)

But presets are recommended for new code.

Templates
../templates/
Commands
../commands/
Configuration
/chat/configuration