chat.AddText(..) Help

I just started a new gamemode and I’m going to have everyone notified on admin join, the only difference is it will be eye candy in the chat. I tried a simple listener server but It doesn’t tell me, I’m not sure if it just shit itself or what. Usually if something doesn’t work i get a lua error, yet I don’t get any errors whatsoever when i do this, I just don’t get the notification, did i do something wrong?

[lua]function TellAdmin( ply )
if ply:IsAdmin() then
for _, v in pairs( player.GetAll() ) do
chat.AddText( v, Color( 0,255,0 ), “[SandBox+]”,Color( 76,168,20 ),“Admin, “…ply:Nick()…”, has joined the game.” )
end
end
end
hook.Add( “PlayerInitialSpawn”, “TellAdmin”, TellAdmin )[/lua]

chat.AddText is a clientside function. If you want to notify all players, use **[BroadcastLua

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.BroadcastLua)** or add the chat.AddText to a clientside script and call it using User Messages.

So this?



function TellAdmin( ply )
if ply:IsAdmin() then
	BroadcastLua( function()
	
		chat.AddText( Color( 0,255,0 ), "[SandBox+]",Color( 76,168,20 ),"Admin, "..ply:Nick()..", has joined the game." )
        
        )

end
hook.Add( "PlayerInitialSpawn", "TellAdmin", TellAdmin )


Couldn’t wrap “lua” because i had to edit the code, sorry.

BroadcastLua() uses a string:

[lua]
function TellAdmin( ply )
if ply:IsAdmin() then
BroadcastLua(‘chat.AddText( Color( 0,255,0 ), “[SandBox+]”,Color( 76,168,20 ),“Admin, “…ply:Nick()…”, has joined the game.”’)

end
hook.Add( “PlayerInitialSpawn”, “TellAdmin”, TellAdmin )
[/lua]

Something that I do not understand is, why do you use ’ and " in certain places, do they not do the same thing?

They are both methods of specifying strings. Having more than one allows us to use strings within strings. To put it simply: the outside one’s will count first, and the inside one’s will be sent through to the chat.AddText function.

[editline]20th July 2011[/editline]
A third way of specifying a string is using [noparse][[ and ]][/noparse]. These can be used on multiple lines:

[lua]
print(“Line 1
Line 2”)
[/lua]

can also be done like this:

[lua]
print([[Line 1
Line 2]])
[/lua]

You really shouldn’t use SendLua, take a look at UserMessages

You can do it through usermessages this way if you wish:

[lua]if SERVER then
AddCSLuaFile( “autorun/FileName.lua” ) --Replace “FileName” with the name you give to this file

local function AdminJoin( ply )
	if !ply:IsAdmin() then return end

	umsg.Start( "AdminText" )
		umsg.String( ply:Nick() )
	umsg.End()
end
hook.Add( "PlayerInitialSpawn", "AdminJoin", AdminJoin )

end

if CLIENT then
local function AdminText( msg )
local AdminName = msg:ReadString()
chat.AddText( Color( 0,255,0 ), “[SandBox+]”,Color( 76,168,20 ),“Admin, “…AdminName…”, has joined the game.” )
end
usermessage.Hook( “AdminText”, AdminText )
end[/lua]

Untested, but create a file in lua/autorun/ then paste in this code. Then change the name at the top of this file to be the same as the name it is saved as

EDIT:

Just noticed you said this was for a gamemode. This will still work, just remove line 2 - “AddCSLuaFile( “autorun/FileName.lua” )”, and place the code into a shared file for simplicity