Jump to content

onChatMessage problem


WolfPire

Recommended Posts

Hi there, guys. It's WolfPire again.

It seems like i have a little problem with a script. It works, the problem is whenever i use "onChatMessage" it also outputs the players name + message. However i have that handled in the function above. How can i get rid of the player's messages output and just leave the resources output?

Server

  
  
function send(text) 
    triggerClientEvent(root, "onSendChat", source, text) 
end 
  
addEventHandler("onPlayerChat", root, send) 
  
function onChatMessageHandler(text, element) 
    if isElement(element) then 
        cancelEvent() 
    else 
        triggerClientEvent("onSendChat", root, text) 
        cancelEvent() 
    end 
end 
  
addEventHandler("onChatMessage", root, onChatMessageHandler) 

Link to comment
So,You made a custom chatbox for players, And you want the normal chatbox to be only for resources or the other way around ?

Nope... I will totally get rid of the chatbox itself...

Here's the deal...

I've already handled players messages... However whenever i try to output the servers messages (resources) they will get output, but also the players message.

I need to find a way to get rid of the players messages or i'll get it doubled.

Link to comment
You can't cancel the onChatMessage event, do it client side onClientChatMessage

Yah, but the problems is not canceling it, the problem is outputing it the data, without outputing also the players message.

Here... In onChatMessage we get:

- Resource's outputChatBox

- MTA Hard-Coded Messages

- Player Messages [X]

- Etc...

The marked one, i want to get rid of it and only receive the rest. Thanks for the info tho', didn't knew it isn't cancelable.

Link to comment

I think you could check if the element is a player by using getElementType, i haven't tested it, but give it a try:

  
function send(text) 
    triggerClientEvent(root, "onSendChat", source, text) 
end 
  
addEventHandler("onPlayerChat", root, send) 
  
function onChatMessageHandler(text, element) 
    if isElement(element) then 
        cancelEvent() 
    else 
    if ( getElementType ( element) ~= "player" ) then 
        triggerClientEvent("onSendChat", root, text) 
        cancelEvent() 
    end 
    end 
end 
  
addEventHandler("onChatMessage", root, onChatMessageHandler) 

Link to comment
I think you could check if the element is a player by using getElementType, i haven't tested it, but give it a try:
  
function send(text) 
    triggerClientEvent(root, "onSendChat", source, text) 
end 
  
addEventHandler("onPlayerChat", root, send) 
  
function onChatMessageHandler(text, element) 
    if isElement(element) then 
        cancelEvent() 
    else 
    if ( getElementType ( element) ~= "player" ) then 
        triggerClientEvent("onSendChat", root, text) 
        cancelEvent() 
    end 
    end 
end 
  
addEventHandler("onChatMessage", root, onChatMessageHandler) 

I tried something similar before... Well thought, itoko! It actually got rid of player messages!

But it wont output the rest :c

Thanks for the help still guys, anyone up to a solution to this?

P.D: Aussi, merci pour le code quand même

EDIT 1: I noticed that all the outputs are linked to the player too, so no matter if the player didn't output anything it will still take the player's message. So i'm on a pretty much fucked situation atm lol

EDIT 2: Since it's not an element, it's a resource, i used "outputDebugString" in order to find out how this was working... And eventually it outputs the resource where everything came from... So i kind of got the solution for this... Just check this pic i just made:

mtascreen20130423223509.png

Actual code right now:

function send(text) 
    triggerClientEvent(root, "onSendChat", source, text) 
end 
  
addEventHandler("onPlayerChat", root, send) 
  
function onChatMessageHandler(text, thing) 
    if isElement(thing) then 
        cancelEvent() 
        outputDebugString(getResourceName ( thing ), 1, 255, 0, 0) 
    else 
        outputDebugString(getResourceName ( thing ), 1, 255, 0, 0) 
        triggerClientEvent("onSendChat", root, text) 
        cancelEvent() 
    end 
end 
  
addEventHandler("onChatMessage", root, onChatMessageHandler) 

EDIT 4: Code after fixing:

function send(text) 
    triggerClientEvent(root, "onSendChat", source, text) 
end 
  
addEventHandler("onPlayerChat", root, send) 
  
function onChatMessageHandler(text, thing) 
    if getResourceName ( thing ) == "freeroam" then 
        cancelEvent() 
        outputDebugString(getResourceName ( thing ), 1, 255, 0, 0) 
    else 
        outputDebugString(getResourceName ( thing ), 1, 255, 0, 0) 
        triggerClientEvent("onSendChat", root, text) 
        cancelEvent() 
    end 
end 
  
addEventHandler("onChatMessage", root, onChatMessageHandler) 

There must be a better way to disable the chat event from the freeroam GUI.

So here's the deal... I blocked freeroam from outputting text, however everything else will get output, however i don't quite get how freeroam manipulates the chatbox in anyway, made me curious and i'll check it later on. So after outputing on the Admin panel "outputChatBox("WolfPire", root, 0, 0, 0)" i get the output correctly. As well the players message output function is working smoothly. So here's a another pic :)

mtascreen20130423225343.png

Wish this info becomes useful for anyone in the future with the same problem :) Thanks for all the support guys, with this, the case can be finally closed!

Again i'd like to thank you for your help and support!

- WolfPire

Link to comment
TIP: I will recommend you to debug your script with outputDebugString, else your server logs will be bigger and it will take longer to start your server. That happened to me ones.

Yeah, it's been over 3 years already coding with LUA for me and i just realized this as i was using outputDebugString :P

Thanks for the advice, this way i'll avoid making my server log a nightmare >w<

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...