Print massages

Hello guys!
I tried to make a simple script which print messages to chat and server console when somebody connect, spawn, leave server.

That’s actually it is:

cl_joinleave.lua



function PlayerJoin( ply )    
   
    local nick = ply:Nick();
    local steam = ply:SteamID();
    
    chat.AddText( "Player ", nick ," (", steam ,") joined." );
    print( "Player "..ply:Nick().." ("..ply:SteamID()..") joined" );

end
usermessage.Hook( "PlayerJoin", PlayerJoin );


function PlayerSpawn( ply )
    
    local nick = ply:Nick();
    local steam = ply:SteamID();
    
    chat.AddText( "Player ", nick ," (", steam ,") spawned." );
    print( "Player "..ply:Nick().." ("..ply:SteamID()..") spawned" );

end
usermessage.Hook( "PlayerSpawned", PlayerSpawned );


function PlayerLeave( ply )
    
    local nick = ply:Nick();
    local steam = ply:SteamID();
    
    chat.AddText( "Player ", nick ," (", steam ,") disconnected." );
    print( "Player "..ply:Nick().." ("..ply:SteamID()..") disconnected" );
    
end
usermessage.Hook( "PlayerLeave", PlayerLeave );


How to make this work in a good way?
What code must be in a sv_joinleave.lua?

That is not how usermessages work. I’d suggest you on reading it up a bit more. The serverside part should be pretty simple for what you’re doing, maximum 10 lines.

You would want to use gamemode hooks for this sort of thing.
GM hooks:
http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexd101.html?title=Gamemode_Hooks
Connect:
http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index0d60.html?title=Gamemode.PlayerConnect
Disc
http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index01a8.html?title=Gamemode.PlayerDisconnected

I made this:



function UserConnect( ply ) 
    for k,v in pairs(player.GetAll()) do 
    v:ChatPrint("Player " .. ply:Nick() .. "(" .. ply:SteamID() .. ") connected.") 
    end
end
hook.Add( "PlayerConnect", "playerconnect", UserConnect )


But server doesn’t want this to work fine.
Print this:



ERROR: Hook 'playerconnect' Failed: [@addons\autorun\lua\auto\sv\sv_messages.lua:3] bad key to string index (number expected, got string)
Removing Hook 'playerconnect'



If you want their steamid you might want to use this:
http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2901.html?title=Gamemode.PlayerInitialSpawn

[lua]
hook.Add( “PlayerInitialSpawn”, “playerInitialSpawn” function(ply)
for k,v in pairs(player.GetAll()) do
v:ChatPrint("Player " … ply:Nick() … “(” … ply:SteamID() … “) connected.”)
end
end)
[/lua]

This tag is called [.lua][./lua] btw