Colored Chat Admin Only

How can I make a colored chat for admins and vips actually!?

What exactly are you looking for? Do you wanna learn how to code it or do you just want the code and to get out of here?

I want to learn how to do that

It would probably be better to use OnPlayerChat clientside to do this so you don’t have to do any networking.

Although then you could just run some client side code that gives you all the text, even if you are not admin or donator.

You could do that either way, nobody cares if you can color the text on your client only.

I think I got it
but can anyone give me the basics
I am thinking of something like this

But don’t know how to do that :confused:
I got this basic can anyone help me?

On your client, you can. Not on the other clients.

Edit:

Here’s a basic template with comments. I didn’t do it all for you, so you will have to fill in some of it.



-- Make a table with keys/values for each usergroup. The group should be the key, the value should be the color.
local usergroups = {
	['admin'] = Color(255,255,0),
	['superadmin'] = Color(255,0,255)
}

-- Here's the hook you need to set up.
hook.Add('OnPlayerChat', 'ColorText', function(ply, text, teamchat, dead)
	-- The best way to do this (IMO) is to add each string/color pair to a table and unpack it at the end.
	if usergroups[ply:GetUserGroup()] then -- Why bother doing this if the user is not a rank that needs to have colored text?
		local chatmsg = {}
		if teamchat then
			chatmsg[#chatmsg+1] = Color(0,255,0)
			chatmsg[#chatmsg+1] = '{TEAM}'
		end
		-- Add the {DEAD} message, same template as above.
		-- Add the admin color (using usergroups[ply:GetUserGroup()] as the entry in the table) and then the name of the player with a colon  and the actual text.
		chatmsg[#chatmsg+1] = usergroups[ply:GetUserGroup()]
		chatmsg[#chatmsg+1] = ply:Name()..': '..text
		-- At the end, unpack the contents of the table as a vararg to the chat.AddText function and return true so it will not show up twice.
		chat.AddText(unpack(chatmsg))
		return true
	end
end)


Thanks but didn`t get this part


-- Add the {DEAD} message, same template as above.
		-- Add the admin color (using usergroups[ply:GetUserGroup()] as the entry in the table) and then the name of the player with a colon  and the actual text.
		
		chatmsg[#chatmsg+1] = usergroups[ply:GetUserGroup()]
		chatmsg[#chatmsg+1] = ply:Name()..': '..text

[editline]26th May 2015[/editline]

Ak to spray,I am kind of new in Lua.
I don`t want the code ready, would you mind to add me in steam and u explain me better
Btw sent u a request :slight_smile:

When I say add the dead message, I mean check if the boolean provided by OnPlayerChat (dead, in this case) is true, and then add a red colored message to the table: ‘{DEAD}’, in the same way we did it with ‘{TEAM}’. In Lua, we have associative arrays, which means that we can have not only numbers as indexes, but also strings and other values. In this case we check the table for the index of the player’s usergroup. If it’s in the table, they are a group that has access to colored chat. Then we add the color for the usergroup (which is the value for the given index) to the table. After that we add the player name and the rest of the text as the next entry to the table; all of the text in that string will be colored whatever color you put as the value to each index.

By the way.
I wanted to thanks everyone in this thread to helping me learn more!

[editline]26th May 2015[/editline]


 if dead then
		chatmsg[#chatmsg+1] = usergroups[ply:GetUserGroup()]
		chatmsg[#chatmsg+1] = ply:Name()..'[DEAD]'..text
		-- At the end, unpack the contents of the table as a vararg to the chat.AddText function and return true so it will not show up twice.
		chat.AddText(unpack(chatmsg))
		return true
	end
end) 

?

[editline]27th May 2015[/editline]

Is this right?

Help please?

Here’s what the “dead” part should look like:



if dead then
	chatmsg[#chatmsg+1] = Color(255,0,0)
	chatmsg[#chatmsg+1] = '{DEAD}'
end


If you didn’t know all the table[#table+1] statement does is add a new entry to the table at the next value. Now that you have the pieces, can you put it together?

Isn’t that just table.insert? :v Or is that more efficient? Never used it

Yes that’s basically what table.insert does, but I think that way is actually a bit more efficient.

So wait, I am kind of lost here.
What should I do in this thing?
I am sorry I am new in to lua, I think that I will learn better


 -- Make a table with keys/values for each usergroup. The group should be the key, the value should be the color.
local usergroups = {
	['admin'] = Color(255,255,0),
	['superadmin'] = Color(255,0,255)
}

-- Here's the hook you need to set up.
hook.Add('OnPlayerChat', 'ColorText', function(ply, text, teamchat, dead)
	-- The best way to do this (IMO) is to add each string/color pair to a table and unpack it at the end.
	if usergroups[ply:GetUserGroup()] then -- Why bother doing this if the user is not a rank that needs to have colored text?
		local chatmsg = {}
		if teamchat then
			chatmsg[#chatmsg+1] = Color(0,255,0)
			chatmsg[#chatmsg+1] = '{TEAM}'
		end
		-- Add the {DEAD} message, same template as above.
		-- Add the admin color (using usergroups[ply:GetUserGroup()] as the entry in the table) and then the name of the player with a colon  and the actual text.
		chatmsg[#chatmsg+1] = usergroups[ply:GetUserGroup()]
		chatmsg[#chatmsg+1] = ply:Name()..': '..text
		-- At the end, unpack the contents of the table as a vararg to the chat.AddText function and return true so it will not show up twice.
		chat.AddText(unpack(chatmsg))
		return true
	end
end) 

It’s functionally the same for numerical tables. It will be ever so slightly faster because you don’t have to go through _G, but it will barely be noticeable unless you call it very often. I use it because using two different types of syntax in the same script looks kind of ugly to me. It’s like using brackets, single quotes, and double quotes in the same script: it just doesn’t look appealing. For example:



local tab1 = {}
local tab2 = {}
tab1['badlookingbelow'] = true
table.insert(tab2, 'see?')


It’s all up to personal preference. Anyway, I really tried not to make it for you, but here you go:



 -- Make a table with keys/values for each usergroup. The group should be the key, the value should be the color.
local usergroups = {
	['admin'] = Color(255,255,0),
	['superadmin'] = Color(255,0,255)
}

-- Here's the hook you need to set up.
hook.Add('OnPlayerChat', 'ColorText', function(ply, text, teamchat, dead)
	-- The best way to do this (IMO) is to add each string/color pair to a table and unpack it at the end.
	if usergroups[ply:GetUserGroup()] then -- Why bother doing this if the user is not a rank that needs to have colored text?
		local chatmsg = {}
		if teamchat then
			chatmsg[#chatmsg+1] = Color(0,255,0)
			chatmsg[#chatmsg+1] = '{TEAM} '
		end
		-- Here's the dead part
		if dead then
			chatmsg[#chatmsg+1] = Color(255,0,0)
			chatmsg[#chatmsg+1] = ' {DEAD| '
		end
		-- Add the admin color (using usergroups[ply:GetUserGroup()] as the entry in the table) and then the name of the player with a colon  and the actual text.
		chatmsg[#chatmsg+1] = usergroups[ply:GetUserGroup()]
		chatmsg[#chatmsg+1] = ply:Name()..': '..text
		-- At the end, unpack the contents of the table as a vararg to the chat.AddText function and return true so it will not show up twice.
		chat.AddText(unpack(chatmsg))
		return true
	end
end)


I feel like people learn less when it’s made for them, but hopefully you’ll actually look at the comments and understand.

I really learn more when I see stuff. Thanks for helping :smiley: