Colored role tags

I want to make a script that when you get the message “You were killed by a Traitor” the word “Traitor” is red, or when “You were killed by a Detective” the word “Detective” is blue, or when “killed by prop/world” the words “prop/world” is grey. This is what I have.

function CustomTTTMsg(ply_or_rfilter, msg, clr)
clr = clr or Color(255, 255, 255, 255)

umsg.Start(“game_msg_color”, ply_or_rfilter)
umsg.String(msg)
umsg.Short(clr.r)
umsg.Short(clr.g)
umsg.Short(clr.b)
umsg.End()
end

hook.Add(“DoPlayerDeath”, “MistRDMDet_NotifyDeaths”, function(ply, attacker, dmginfo)

if IsValid(attacker) and attacker:IsPlayer() then
if attacker == ply then
CustomTTTMsg(ply, “You killed yourself”, COLOR_GREEN)
else
local clr = attacker:GetRole() == ROLE_TRAITOR and COLOR_RED or COLOR_GREEN
CustomTTTMsg(ply, "You were killed by a " … attacker:GetRoleString(), clr)
end
else
CustomTTTMsg(ply, “You were killed by <something/world>”, COLOR_WHITE)
end

end)

Could someone help me out?

What is it exactly you need help with? Is there something you don’t know how to do? Are you getting errors you haven’t pasted here? Help us help you.

I dont know how to color the words.

Send a usermessage to the client then just use chat.AddText.

Haven’t really worked with usermessages in months, but this should work.



if( !CLIENT ) then return; end

usermessage.Hook( "game_msg_color", function( um )
	local msg = um:ReadString();
	local clr = Color( um:ReadShort(), um:ReadShort(), um:ReadShort(), 255 );
	chat.AddText( clr, msg );
end );


What would that do? Because i dont want their usernames to be red, i want the word “traitor” in the message “you have been killed by a traitor” to be red

[editline]26th November 2012[/editline]

and where would I add that?

You would have to change you code around to look something like this.



function CustomTTTMsg(ply_or_rfilter, attackertype, clr)
	clr = clr or Color(255, 255, 255, 255)

	umsg.Start("game_msg_color", ply_or_rfilter)
		umsg.String(attackertype)
		umsg.Short(clr.r)
		umsg.Short(clr.g)
		umsg.Short(clr.b)
	umsg.End()
end

hook.Add("DoPlayerDeath", "MistRDMDet_NotifyDeaths", function(ply, attacker, dmginfo)
	if IsValid(attacker) and attacker:IsPlayer() then
		if attacker == ply then
			CustomTTTMsg(ply, "yourself", COLOR_GREEN)
		else
			local clr = attacker:GetRole() == ROLE_TRAITOR and COLOR_RED or COLOR_GREEN
			CustomTTTMsg(ply, "a " .. attacker:GetRoleString(), clr)
		end
	else
		CustomTTTMsg(ply, "<something/world>", COLOR_WHITE)
	end
end)

And your clientside usermessage hook:


usermessage.Hook("game_msg_color", function(um)
	local attkr = um:ReadString()
	local col = Color(um:ReadShort(), um:ReadShort(), um:ReadShort())
	chat.AddText("You were killed by ", col, attkr, ".")
end)

If that ‘CustomTTTMsg’ or the usmg is used by other stuff, change the name.

Thankssomuch!!

[editline]26th November 2012[/editline]

Do they need a specific title?

you can be more efficient with usermessages by using umsg.Char for the color

So banana lord how would i change the script to be moar efficient?

[editline]26th November 2012[/editline]

and hemirox it doesnt seem to be reading the colors, like it prints “killed by a traitor” but traitor isnt red

well it really doesn’t matter in this case but if you umsg.Char( clr.r - 127 ) and then when receiving it add 127 back you get your number

in regards to your color, is COLOR_RED defined? as far as I know the only defined colors are color_white and color_black

So how would i define it to be red?

Most colors are defined by TTT, including COLOR_RED, but if you want, red is Color( 255, 0, 0, 255 ).

So if the color is already defined what would the error be? Because the words are just plain white, but they are saying the right thing almost as if it isnt reading the colors

Here is what I have:
Server:

function CustomTTTMsg(ply_or_rfilter, attackertype, clr)
clr = clr or Color(255, 255, 255, 255)

    umsg.Start("game_msg_color", ply_or_rfilter)
            umsg.String(attackertype)
            umsg.Short(clr.r)
            umsg.Short(clr.g)
            umsg.Short(clr.b)
    umsg.End()

end

hook.Add(“DoPlayerDeath”, “MistRDMDet_NotifyDeaths”, function(ply, attacker, dmginfo)
if IsValid(attacker) and attacker:IsPlayer() then
if attacker == ply then
CustomTTTMsg(ply, “yourself”, COLOR_GREEN)
else
local clr = attacker:GetRole() == ROLE_TRAITOR and COLOR_RED or COLOR_GREEN
CustomTTTMsg(ply, "You were kill by a " … attacker:GetRoleString(), clr)
end
else
CustomTTTMsg(ply, “<something/world>”, COLOR_WHITE)
end
end)

and the clientside hook

usermessage.Hook(“game_msg_color”, function(um)
local attkr = um:ReadString()
local col = Color(um:ReadShort(), um:ReadShort(), um:ReadShort())
chat.AddText("You were killed by ", col, attkr, “.”)
end)

It is understanding the message but the words aren’t coming out colored, help please?

[editline]27th November 2012[/editline]

Oh I see the problem, the text isn’t appearing in the chat box, it is in the top right corner of the screen, would that change anything?

Why don’t you use ttts messaging system?

So this works for traitor, innocent, suicide, prop/world, but it doesnt for detective, does anyone know how to make detective blue?

Use ROLE_DETECTIVE

I haven’t looked into it, because I don’t play TTT, but I believe there is a Player:IsDetective function.

Edit:

Yeah, it does exist. Read through the gamemode files to learn more.