Simple code snippet returns error

function PlayerJoinedSendName( ply, ip )

	local name = ply:Nick()
	print( "Player " .. name .. " is connecting to the server" )

	util.AddNetworkString( "PlayerNameSend" )
	net.Start( "PlayerNameSend" )
	net.WriteString( name )
hook.Add( "PlayerConnect", "PlayerJoinedTheServer", PlayerJoinedSendName )

Returns this error:

[ERROR] addons/custom_joinleave_msgs/lua/autorun/joinleavemessages/sv_msgs.lua:9
: bad key to string index (number expected, got string)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:274
    3. fn - addons/custom_joinleave_msgs/lua/autorun/joinleavemessages/sv_msgs.l
     4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

No player argument is given to the PlayerConnect hook. A name however, is.

Ahh, I see, thank you!

Also, you only need to (and should) call util.AddNetworkString once for each net message, preferably when the script loads, so it can be moved outside of your callback.

Also, use net.Broadcast instead of net.send if you are wanting to tell the entire server someone is connecting as you currently have no one receiving the net message.

Awesome, I thought I would’ve had to do a for loop for chat.AddText for it to display to everyone.