Help with understanding chat


I wanted to write a simple resource to add some commands for adding weapons and spawning vehicles etc (first resource i create). I was looking in the [system]\chat resource to see how to do it and now i’m very confused.

My understanding is this:
When something is typed in the chat window, the following callback is made in the client script:

RegisterNUICallback('chatResult', function(data, cb)
  chatInputActive = false
  if not data.canceled then
    local id = PlayerId()
    local r, g, b = 0, 0x99, 255
    if data.message:sub(1, 1) == '/' then
      TriggerServerEvent('_chat:messageEntered', GetPlayerName(id), { r, g, b }, data.message)

After this the server runs the following event handler:

AddEventHandler('_chat:messageEntered', function(author, color, message)
    if not message or not author then
    TriggerEvent('chatMessage', source, author, message)
    if not WasEventCanceled() then
        TriggerClientEvent('chatMessage', -1, author,  { 255, 255, 255 }, message)
    print(author .. ': ' .. message)

This triggers a chatMessage server event and also a chatMessage client event.

Based on this i now have a couple of questions:

  1. How does cancellation of events work? I would have thought that if i cancel an event it would stop further eventhandlers from being called on it, but the use of WasEventCancelled() suggests that an event handler will be called even if the event is cancelled?

  2. The server triggers a server chatMEssage event, but no such event is registered by the server, is there only a need to register events that gets called between server and client?

  3. In the client code (cl_chat.lua) chatResult callback, if the first character is a ‘/’, it doesnt trigger a _chat:messageEntered event but instead calls an ExecuteCommand function on the input. What does this do/where can i find it? And does that mean that nothing automatically gets sent to the server if i start a message with a ‘/’?

  4. I’m curious how event handlers handle different arguments. Am i correct in thinking that if i trigger a particular event with some arguments, any eventhandlers that match those arguments will get triggered, and if i later trigger the same event with different arguements it will trigger different handlers? As in, when triggering event handlers it pattern matches the argument types?

Thanks for any help on this!


To make these you don’t even have to use the chat resource. Take a look on the Wiki for types of events


Well i am definitely going to want various commands later anyway so i might as well figure it out now.


Just so you know. To make a new command you don’t need to use the chat resource to register it, if that makes sense. You would use the RegisterCommand in your script