Using net in join message

I am attempting to make a join message as i prefer to make my own little scripts rather than using somebody else’s.
I’m having trouble getting this text from the client to user as chat.AddText is clientside



local function PlayerJoined(ply)
	if ply:IsPlayer() then
hook.Add("PlayerInitialSpawn","spawnmsg", PlayerJoined)

	chat.AddText(Color(255,0,0),ply:GetName(),Color(255,255,255)," has joined the server

I am getting the following errors:

[ERROR] addons/maverick_joinmsg/lua/autorun/cl_netlib.lua:2: attempt to index global 'ply' (a nil value)
  1. func - addons/maverick_joinmsg/lua/autorun/cl_netlib.lua:2
   2. unknown - lua/includes/extensions/net.lua:32

[ERROR] addons/maverick_joinmsg/lua/autorun/sv_init.lua:3: attempt to call field 'AddNetworkString' (a nil value)
  1. unknown - addons/maverick_joinmsg/lua/autorun/sv_init.lua:3

Do you want a message to be printed when a player spawns or a message that is broadcasted to all players?

You’re sending the join message to just the person who joined, instead of everyone. Use net.Broadcast() instead of net.Send(ply)

EDIT: You also need to send data about who is joining like wauterboi said

a message that is broadcasted to all players

Well, for starters, you’re sending it to only one person, which is the player that joins. Secondly, you aren’t sending any data, and so when the client receives the message, it has no idea what “ply” is.

You should do the following:

  1. After net.Start(), use net.WriteEntity() or net.WriteString() to send information about the entity or player.
  2. Use net.Broadcast() instead of net.Send() to send the information to all players.
  3. In net.Receive(), save the result of net.ReadEntity() or net.ReadString() to a variable and read from that.

For example, if you wrote an entity, you could read it into a variable called “ply” and the line you have now should work. (Although I believe it’s Nick(), not GetName(). Dunno, just writing off the cuff!) But if you wrote a string, you could save that to a variable called Nick and replace “ply:GetName()” with that.

Cleaned up the code for you and added comments so you should be able to understand how everything works.

--Check if the code is running serverside.
if SERVER then

	--Download the current file to the client.

	--Pool network string to be sent.

	--Add a hook for when a player connects.
	hook.Add("PlayerInitialSpawn","spawnmsg", function(ply)
		net.Start("UniqueName_colorMsg") -- Start a new netmessage.
		net.WriteString(ply:Name()) -- Send the name of the player to the client.
		net.Broadcast() -- Send the network message to all players.

else -- If the code is running clientside.

	--Add receiver for UniqueName_colorMsg

		--Read the data sent from the server and save it into a local var.
		local ply = net.ReadString()

		--Print the message to the chat.
		chat.AddText(Color(255,0,0),ply,Color(255,255,255)," has joined the server


You can read more about the Net Library here.

Thanks a lot :slight_smile: