Jump to content

onPlayerChat


Recommended Posts

Hello, i have a problem here, when i try to chat in team chat, everybody can see the message that i wrote and same thing happening to other teams chat, If you don't understand what i mean, is that when i write in team chat, everybody can read the message like local chat, hope somebody help, thanks.

function RGBToHex(red, green, blue) 
    if((red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255)) then 
        return nil 
    end 
    return string.format("#%.2X%.2X%.2X", red, green, blue) 
end 
  
local function onplayerChat ( message, messageType ) 
    if ( messageType == 2 ) then --Team (Clan) chat 
        cancelEvent ( ) 
        local name = getPlayerName ( source ) 
        local red, green, blue = getPlayerNametagColor ( source ) 
        local hex = RGBToHex ( red, green, blue ) 
        
        outputChatBox( "#9AFE2E(CLAN)" .. hex.." "..name .. ":#FFFFFF" .. message, root, 255, 255, 255, true ) 
        outputServerLog( "(TEAM): " .. name .. ": " .. message )--NOTE: Beacuse we cancelled the onPlayerChat event, we need to log chat manually. 
    end 
end 
addEventHandler ( "onPlayerChat", root, onplayerChat ) 
         
function nickChangeHandler() 
    if isPlayerMuted ( source ) then 
        outputChatBox("You are muted.", source, 100, 0, 0) 
        cancelEvent() 
    end 
end 
addEventHandler("onPlayerChangeNick", getRootElement(), nickChangeHandler) 

Link to comment
  • Moderators

You were displaying the message to root (which means everyone).

local function onplayerChat ( message, messageType ) 
    if ( messageType == 2 ) then --Team (Clan) chat 
        cancelEvent ( ) 
        local name = getPlayerName ( source ) 
        local red, green, blue = getPlayerNametagColor ( source ) 
        local hex = RGBToHex ( red, green, blue ) 
  
        local team = getPlayerTeam( source ) 
        if not team then 
            return outputChatBox( "You are not part of a team yet !", source, 200, 0, 0) 
        end  
  
        -- We have to get all the team members instead of using root 
        local members = getPlayersInTeam( team ) or {} 
  
        outputChatBox( "#9AFE2E(CLAN)" .. hex.." "..name .. ":#FFFFFF" .. message, members, 255, 255, 255, true ) 
        outputServerLog( "(TEAM): " .. name .. ": " .. message ) 
    end 
end 
addEventHandler ( "onPlayerChat", root, onplayerChat ) 

Link to comment

I think outputChatBox doesn't accept table, so in case Citizen code doesn't work, try this:

local function onplayerChat ( message, messageType ) 
    if ( messageType == 2 ) then --Team (Clan) chat 
        cancelEvent ( ) 
        local name = getPlayerName ( source ) 
        local red, green, blue = getPlayerNametagColor ( source ) 
        local hex = RGBToHex ( red, green, blue ) 
  
        local team = getPlayerTeam( source ) 
        if not team then 
            return outputChatBox( "You are not part of a team yet !", source, 200, 0, 0) 
        end 
  
        -- We have to get all the team members instead of using root 
        local members = getPlayersInTeam( team ) or {} 
        for _, player in ipairs(members) do 
            outputChatBox( "#9AFE2E(CLAN)" .. hex.." "..name .. ":#FFFFFF" .. message, player, 255, 255, 255, true ) 
        end 
        outputServerLog( "(TEAM): " .. name .. ": " .. message ) 
    end 
end 
addEventHandler ( "onPlayerChat", root, onplayerChat ) 

Link to comment
  • Moderators
I think outputChatBox doesn't accept table

Well, you are probably right, I don't know why but I thought getRootElement() was returning a table. :oops:

But if the outputChatBox is looking for the children of the element (like root for example) we can then probably create an element and put the team members as children of it and then send that element as argument.

(I know ur loop is a way better, it's just to share this possible solution.)

But then, if the team element has the members as children, we can probably try to do an outputChatBox on team instead of root :o

I'll test this out this week-end.

Link to comment

Dude, you know how it works? i will be 1,2,3,4,5 (Or until whatever number the count returns) and it will work..

Eg:

  
for i=1, 10 do 
outputChatBox(i, getRootElement()) 
end 
  

 

Will output:

 

  
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
  

Anyways yes, it wont work if based on a team and not a table.

  
for i,v in ipairs(getPlayersInTeam(TEAM)) do 
outputChatBox("text", v) 
end 
  

But if table members..

  
for i=1, #members do 
outputChatBox("text", members[i]) 
end 
  

Link to comment
Dude, you know how it works? i will be 1,2,3,4,5 (Or until whatever number the count returns) and it will work..

Eg:

  
for i=1, 10 do 
outputChatBox(i, getRootElement()) 
end 
  

 

Will output:

 

  
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
  

Anyways yes, it wont work if based on a team and not a table.

  
for i,v in ipairs(getPlayersInTeam(TEAM)) do 
outputChatBox("text", v) 
end 
  

But if table members..

  
for i=1, #members do 
outputChatBox("text", members[i]) 
end 
  

Read your code again.

outputChatBox ( "", i) -- Do you see it now? the "i" is in the second arg which is totally wrong.

_____________________________________________________________________________________________________________

for i=1, 10 do 
outputChatBox(i, getRootElement()) 
end 

And yes this will work ofc, I never said anything about this.

Link to comment

The second argument in outputChatBox is optional with root as default value, passing any argument that isn't acceptable will either generate errors or output your text to root. There might be a solution to pass a team as argument if you can convert it to an element tree structure. Otherwise it will most likely fall back to root and the team messages keeps showing for all players online. I would recommend the loop based on the table of team members that's probably the easiest solution.

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...