Add to chat box from server, with color?

Hey, does anyone know of a function that adds text to a players chat box, FROM THE SERVERSIDE, that has support for COLOR?

chat.AddText can only be called from the clientside, so I can’t use that, and it’s the only function I know of that supports color.

What do you want to do ?

You can’t add it from the Serverside directly, it has to be done on the Clienside. In order to do so, you need to send a Net/User Message from Server to client.

ply:ChatPrint(“HI”)

Something like that, except I can color the text.

[editline]2nd June 2013[/editline]

Yeah, I tried that, however I’m sending data to chat.Addtext, and the data doesn’t get sent as fast as the message gets triggered, so the data is nil on the clientside.

You need to look into net messages, yes chat.AddText is clientside, but the way you need to run it is by sending a net message to the player to add it.

I just wrote this quickly, you should try something like this, but obviously adapt it to your own needs:


if SERVER then
	util.AddNetworkString( "YouDied" ) -- adding the net message, this name must be the same as the name you set later
	function AddTextOnDeath( ply )
		net.Start( "YouDied" ) -- naming the net message
		net.Send( ply ) -- sending to the player, you can use net.Broadcast() to send to everyone
	end
	hook.Add( "PlayerDeath", "AddtoChat", AddTextOnDeath )
end

if CLIENT then
	net.Receive("YouDied", function() -- here you received the net message, using the name set earlier
		chat.AddText(Color(255,0,0),"You have died!") -- and here you can use your chat.AddText(), with which ever colours you need
	end)
end 

-EDIT-

To combat this when you receive your net message you will probably set your variable with the new information, then you must later say var = (what you set it to when you received the net message) or 0/"" so that it has a backup to set it to before the net receives and thus stopping any errors.

I’ve fixed it, I was doing something really stupid. I was setting the data as a global string, and then sending a user message that triggers the text, that prints that global string. These two net functions send at different speeds.

I had a hud element that needed a global string because when you leave and come back, it has to still be there, so I tried to do the same thing for the one time messages, but they don’t need to do that because… they are one time, not constant like the HUD.

Thanks!

Uhh, I wouldn’t use net for this as it just over-complicates things.



BroadcastLua("chat.AddText(Color(100, 100, 100), 'MESSAGE HERE'")


Easy & simple. Could even write a function for it like so (untested, but should work)



function SendChatMessage(colour, message)
    BroadcastLua("chat.AddText(" .. colour .. "," .. message .. ")")
end


You’ll actually end up with more networking this way

Don’t use BroadcastLua for this, it has length limit of 254 or so IIRC.

[lua]if CLIENT then
net.Receive( “serverAddText”, function()
local tab = net.ReadTable()
chat.AddText( unpack(tab) )
end )
return
end

AddCSLuaFile()
util.AddNetworkString( “serverAddText” )

chat = {}
function chat.AddText( … )
local arg = {…}
local ply
if type(arg[1]) == “player” then
ply = table.remove( arg, 1 )
end

net.Start( "serverAddText" )
	net.WriteTable( arg )
if IsValid(ply) then net.Send(ply) else net.Broadcast() end

end[/lua]

Server-sided chat.addtext

(credits to mr gash)

How would it end up with more networking then? Just curious.

It is not, but it is subject to injection if you passed “…error()…” as the message.

Each character for BroadcastLua will add 1 byte to the amount networked, “chat.AddText(Color(255,255,255), ‘message’)” is 43 bytes. Using net we can write 3 8 bit unsigned integers for the color which is 3 bytes. Including the the string we get to 7 bytes + an integer containing the length (let’s call that integer 4 bytes).

That example leaves us with 14 bytes opposed to 43, becoming more efficient the more data you wish to send.

Didn’t realise that. I’m just going to fix my gamemode…

Thanks, didn’t really think much about efficiency (or security for that matter).