[Release] Cancel Chat

Hello,

This is a quick release that I made after seeing it requested multiple times.

Simply do the command “/cancelchat” and it will disable chat for everyone.

Download: Cancel Chat.zip (875 Bytes) (Code might be a bit messy because it is a simple script done quickly)

Warning: You DO need your own whitelist system for it. I might add it at one point but I probably will not update this.

Hope you enjoy,
Scotty

7 Likes

Hello,
Could you do a script like DOJ where they do “/textchat” and it turns off text chat and if you do it again, it will turn it back on. It needs to only be disabled for that specific user?

Many Thanks,
Kyle.

RegisterCommand you might want to use it.
Also local cchat = false. Localize variables if possible.

If you release more stuff, you might want to start programming in a clean fashion.

Also it has the bug that it replys for anyone that they canceled chat themselves.

Just saying, keep on improving!

https://github.com/citizenfx/cfx-server-data/pull/12 Try this for that. It’s what I’m using

Thank you. Means alot!

1 Like

I did not use RegisterCommand so that everything could be in one file. I usually do.

you do realise RegisterCommand is both for client and server? you do not need another file

1 Like

Oh yeah, I completely forgot lol.

Edited download to clean up code.

the togglechat command does not work from the resource you linked to

1 Like

It is not a resource :wink: I will work on making one anyway.

I don’t think it will work the way you have it now. I think you want to cancel the chat events. But use a command after which all chat events are canceled.

So something like that:

local cancelchat = false
local name = ''

RegisterCommand('cancelchat', function(source, args, rawCmd) 
	cancelchat = not cancelchat
	name = GetPlayerName(source)
	local msg = name .. ' has '
	if cancelchat then
		msg = msg .. 'canceled chat for everyone.'
	else
		msg = msg .. 'enabled chat for everyone.'
	end
	TriggerClientEvent('chatMessage', -1, 'Cancel Chat', {255, 255, 255}, msg)
end, false)

AddEventHandler('chatMessage', function(source)
	if cancelchat then
		TriggerClientEvent('chatMessage', source, 'Cancel Chat', {255, 255, 255}, name .. ' has canceled chat for everyone, your message was deleted.')
		CancelEvent()
	end
end)

Not sure if it actually works. That is your responsibility.

this is my client chat file.
it disables the chat client side by using /textchat.

maybe you can implement this serverside and do it for everyone?

local chatInputActive = false
local chatInputActivating = false
local chatVisibilityToggle = false

RegisterNetEvent('chatMessage')
RegisterNetEvent('chat:addTemplate')
RegisterNetEvent('chat:addMessage')
RegisterNetEvent('chat:addSuggestion')
RegisterNetEvent('chat:removeSuggestion')
RegisterNetEvent('chat:clear')
RegisterNetEvent('chat:toggleChat')

-- internal events
RegisterNetEvent('__cfx_internal:serverPrint')

RegisterNetEvent('_chat:messageEntered')

--deprecated, use chat:addMessage
AddEventHandler('chatMessage', function(author, color, text)
  local args = { text }
  if author ~= "" then
    table.insert(args, 1, author)
  end

  if(not chatVisibilityToggle)then
    SendNUIMessage({
      type = 'ON_MESSAGE',
      message = {
        color = color,
        multiline = true,
        args = args
      }
    })
  end
end)

AddEventHandler('__cfx_internal:serverPrint', function(msg)
  print(msg)

  if(not chatVisibilityToggle)then
    SendNUIMessage({
      type = 'ON_MESSAGE',
      message = {
        color = { 0, 0, 0 },
        multiline = true,
        args = { msg }
      }
    })
  end
end)

AddEventHandler('chat:addMessage', function(message)
  if(not chatVisibilityToggle)then
    SendNUIMessage({
      type = 'ON_MESSAGE',
      message = message
    })
  end
end)

AddEventHandler('chat:addSuggestion', function(name, help, params)
  SendNUIMessage({
    type = 'ON_SUGGESTION_ADD',
    suggestion = {
      name = name,
      help = help,
      params = params or nil
    }
  })
end)

AddEventHandler('chat:removeSuggestion', function(name)
  SendNUIMessage({
    type = 'ON_SUGGESTION_REMOVE',
    name = name
  })
end)

AddEventHandler('chat:addTemplate', function(id, html)
  SendNUIMessage({
    type = 'ON_TEMPLATE_ADD',
    template = {
      id = id,
      html = html
    }
  })
end)

AddEventHandler('chat:clear', function(name)
  SendNUIMessage({
    type = 'ON_CLEAR'
  })
end)

AddEventHandler('chat:toggleChat',function()
  chatVisibilityToggle = not chatVisibilityToggle

  local state = (chatVisibilityToggle == true) and "^1disabled" or "^2enabled"

  SendNUIMessage({
    type = 'ON_MESSAGE',
    message = {
        color = {255,255,255},
        multiline = true,
        args = {"Chat Visibility has been "..state}
      }
    })
end)

RegisterCommand("textchat",function()
  TriggerEvent('chat:toggleChat')
end)

RegisterNUICallback('chatResult', function(data, cb)
  chatInputActive = false
  SetNuiFocus(false)

  if not data.canceled then
    local id = PlayerId()

    --deprecated
    local r, g, b = 0, 0x99, 255

    if data.message:sub(1, 1) == '/' then
      ExecuteCommand(data.message:sub(2))
    else
      TriggerServerEvent('_chat:messageEntered', GetPlayerName(id), { r, g, b }, data.message)
    end
  end

  cb('ok')
end)

RegisterNUICallback('loaded', function(data, cb)
  TriggerServerEvent('chat:init');

  cb('ok')
end)

Citizen.CreateThread(function()
  SetTextChatEnabled(false)
  SetNuiFocus(false)

  while true do
    Wait(0)

    if not chatInputActive then
      if IsControlPressed(0, 245) --[[ INPUT_MP_TEXT_CHAT_ALL ]] then
        chatInputActive = true
        chatInputActivating = true

        SendNUIMessage({
          type = 'ON_OPEN'
        })
      end
    end

    if chatInputActivating then
      if not IsControlPressed(0, 245) then
        SetNuiFocus(true)

        chatInputActivating = false
      end
    end
  end
end)

Register command can bs used on client and server :slight_smile:. I usually still dont use it on client though.

is there a proper working release for this that disables textchat localy for the user?

this dont work chief

You could also without the script do “F8” and type “stop chat”

I don’t know what this changes