Jump to content

[HELP] Nametag color in chat


Recommended Posts

Hello, I have used the function setPlayerNametagColor to set the color of the player's nametag according to his ACL rank, but I would also like the color in chat to be changed as to grant more visibility to both staff and vip players. The problem is that I have been searching for similar topics but found nothing usable to start scripting such a function, so I would like someone to give me an idea of what to use in order to make that possible.

Edited by Galactix
Link to comment
  • Discord Moderators

Hello, because you put effort in it, I'll write a code for ya'.
 

local HEX_TABLE = {[0] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"}
local ENABLED_MSG_TYPES = {[0] = true}

local function rgb2hex(r,g,b)
    return "#"..HEX_TABLE[math.floor(r/16)]..HEX_TABLE[r-(16*r1)]..HEX_TABLE[math.floor(g/16)]..
                HEX_TABLE[g-(16*g1)]..HEX_TABLE[math.floor(b/16)]..HEX_TABLE[b-(16*b1)]
end

addEventHandler("onPlayerChat", root, 
    function(msg, msgType)
        if (not DISABLED_MSG_TYPES[msgType]) then
            outputChatBox(rgb2hex(getPlayerNametagColor(source))..msg)
        end
    end
)

Untested.

Link to comment
addEventHandler("onPlayerChat", root, 
    function(msg, msgType)
        if msgType == 0 then
            local r,g,b = getPlayerNametagColor( source )
            local name = getPlayerName( source ):gsub("#%x%x%x%x%x%x","") -- remove cc from name
      	    local msg = msg:gsub("#%x%x%x%x%x%x","")
            outputChatBox(name.."#FFFFFF: "..msg,r,g,b,true)
        end
    end
)

 

Edited by Mr.Loki
Link to comment

@Mr.Loki I didn't meant it for message color, but for the nametags color set up with the setPlayerNametagColor function. It doesn't appear in chat, only in scoreboard and under the player's health.

By the way, how to use the account name for display instead to prevent players from using hexacode colors?

Edited by Galactix
Link to comment
5 hours ago, Galactix said:

@Mr.Loki I didn't meant it for message color, but for the nametags color set up with the setPlayerNametagColor function. It doesn't appear in chat, only in scoreboard and under the player's health.

That's what i did in the code. i only changed the Name color in the messages to work with setPlayerNametagColor.

5 hours ago, Galactix said:

By the way, how to use the account name for display instead to prevent players from using hexacode colors?

The code I posted shows you how to remove CCs(Color codes) from names and messages.

Link to comment

Well, using your script doesn't color nametags and doesn't cancel any hexcolor coded nametag without giving any error.

I have however found this on the wiki for the outputChatBox function which works for the nametag color on players without hexacode colors:

function colouredChat ( message, theType )
	if theType == 0 then --if its normal chat (not /me or teamchat) then
		cancelEvent() --prevent MTA from outputting chat
		message = string.gsub(message, "#%x%x%x%x%x%x", "") --remove any hex tags in a player's chat to prevent custom colours by using lua's string.gsub
		local r,g,b = getPlayerNametagColor ( source ) --get the player's nametag colour
		local chatterName = getPlayerName ( source ) --get his name
		--output a message with the name as his nametag colour, and the rest in white.
		outputChatBox ( chatterName..":#FFFFFF "..message, getRootElement(), r, g, b, true )
	end
end
addEventHandler("onPlayerChat", getRootElement(), colouredChat)

except that the player using nicknames containing hexcolor codes aren't affected by it, why?

Edited by Galactix
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...