PrintMessage

Hello, I’m just asking to make sure, What i want to do is printing a message to specified player’s chat with colors, here is my simple code:

[lua]
– Which one of these work? this one below?
ply:PrintMessage( HUD_PRINTTALK, Color(255, 0, 153, 255)…“Testing the color” )
– Or this one?
ply:PrintMessage( HUD_PRINTTALK, Format( “&sTesting the color”, Color(255, 0, 153, 255) ) )
[/lua]

And is the HUD_PRINTTALK for printing on the player’s chat box?
Thank you!

First argument has to be a number, in your case 3 instead of “HUD_PRINTTALK”.


ply:PrintMessage(3, "YOURSTRING")

Have no idea about the color though, but you could just make a usermessage and use chat.AddText.

Why? HUD_PRINTTALK works fine?

It would have to be like this, [lua]ply:PrintMessage( HUD_PRINTTALK,“Testing the color” )[/lua]
as PrintMessage does not have a colour function, if you want colour try something like this:

[lua] util.AddNetworkString(“NetMessageName”)
if SERVER then
net.Start(“NetMessageName”)
net.Send(ply)
else
net.Receive(“NetMessageName”, function()
chat.AddText(Color(255,0,0),“Testing the colour”)
end)
end[/lua]

Possible to make it like a function that i can use for sending to a player? Thanks


function Shit(ply)
 util.AddNetworkString("NetMessageName")
if SERVER then
	net.Start("NetMessageName")
	net.Send(ply)
else
	net.Receive("NetMessageName", function()
		chat.AddText(Color(255,0,0),"Testing the colour")
	end)
end
concommand.Add("SendShit",Shit)

Make sure its shared.


if SERVER then
        util.AddNetworkString("NetMessageName")
	net.Start("NetMessageName")
	local tbl = {Color(255,0,0),"Testing the colour"} -- Put your shit inside of this table.

	net.WriteTable(tbl)

	net.Send(ply)
else
	net.Receive("NetMessageName", function()
		chat.AddText(unpack(net.ReadTable()))
	end)
end

Disregard all previous replies.

Serverside:



util.AddNetworkString( "PrintColor" )

local Meta = FindMetaTable( "Player" )

function Meta:PrintColor( ... )

	net.Start( "PrintColor" )
		net.WriteTable( { ... } )
	net.Send( self )
end

Clientside:



net.Receive( "PrintColor", function()
	chat.AddText( unpack( net.ReadTable() ) )
end )

Example usage:



Warlock:PrintColor( Color( 255, 0, 0 ), "Roses are red, ", Color( 0, 0, 255 ), "violets are blue, ", Color( 255, 255, 255 ), "this is white and so are you!" )

It’d actually be more efficient to not send it as a table, but as separate values, but yeah… It’s not really a performance intensive task.

Solved :slight_smile:

If you’re interested in how:

shared (SERVER & CLIENT) File:
[lua]
local meta = FindMetaTable(“Player”)
function meta:Send(msg)
umsg.Start( “Send_Print”, self )
umsg.String( msg )
umsg.End()
end

function SendAll(msg)
umsg.Start( “Send_Print” )
umsg.String( msg )
umsg.End()
end
[/lua]

cl_init (CLIENT) File:
[lua]
usermessage.Hook( “Send_Print”, function( data )

chat.PlaySound()
   -- Prints in white
chat.AddText( Color( 255, 255, 255, 255 ), (data:ReadString() or "This is a random message!") )

end )
[/lua]

Psst: I was a noob 3 weeks ago with CLIENT and SERVER files stuffs :slight_smile: