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
hook.Add( "PlayerDeath", "AddtoChat", AddTextOnDeath )
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
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.
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.