Join Leave

I was trying to make a lua plugin that tells you when a player joins, and the Name and steam-ID of the player when they leave. It doesn’t work for some reason


function GM:PlayerDisconnected( ply )
	PrintMessage( HUD_PRINTTALK, ply:Name().. " has left the server his steam id is".. ply:SteamID() )
end

function GM:PlayerConnect( name, ip )
	PrintMessage( HUD_PRINTTALK, name.. " has joined the game." )
end


I put it in autorun/server so it would automatically execute the lua file

( I’m sorry if i am posting in the wrong area(1st Time))

You should be using hook.Add instead of overriding the gamemode functions, like this:

[lua]
hook.Add(“PlayerDisconnected”, “customidentifier_changethis”, function( ply )
PrintMessage( HUD_PRINTTALK, ply:Name()… " has left the server his steam id is"… ply:SteamID() )
end)

hook.Add(“PlayerConnect”, “customidentifier_changethis”, function( name, ip )
PrintMessage( HUD_PRINTTALK, name… " has joined the game." )
end)
[/lua]

As for it not actually working, is that the entire file? And where is the file?

that is the entire file( I’m a beginner at lua )

and when you put _changethis( im not sure if that suppose to be changed or not)

That literally means he wants you to change “customidentifier_changethis” to your own word :v:

so what would i put there (as an example) and he also said that it is not complete what else do i need to add

Something that you think would not be overriden by anything else. For example
BMG_DisconnectNotice and BMG_ConnectNotice

oh ok but how would I make it so that it says the words in different color like [green] BMG[green] has joint the server or BMG has left the serve is steam id is (steamID)

You would use the http://wiki.garrysmod.com/page/Net_Library_Usage Net Library and

chat.AddText chat.AddText

ok so after I add all that the code will work

Uh, I don’t think you quite understand this… read up on the examples on the net library page a bit.
Here’re the basics of what you want to do:
In the server hooks, broadcast a net message to all clients. Write the name and ip with the net message.
On the client, receive that message and call chat.AddText with your color or whatever and the received data.

could you possibly write an example code please(like I said i’m a beginner(sorry))

Sure.
[lua]
if SERVER then – if we’re on the server
util.AddNetworkString(“Example1”) – You must call this on the name of the net message before we can send it!

hook.Add("PlayerInitialSpawn", "NetLibraryTest_PlayerInitialSpawn", function(ply) -- add a hook to player initially spawning
	net.Start("Example1") -- begin our net message
		net.WriteString(ply:Nick()) -- write the player's name, which is a string
	net.Broadcast() -- send the message to all connected players
end)

else – we’re on the client
net.Receive(“Example1”, function(len) – set up a receiver for the message
local str = net.ReadString() – read the string we were sent
chat.AddText(str) – printthe str to the chat
end)
end
[/lua]
I’ve commented the code so you can see all that’s happening. This contains both client and serverside code, so this must go in a shared file. For example, lua/autorun/whatever.lua is loaded by both client and server.
What this does:
When a player spawns for the first time (playerinitialspawn) we send a net message to all connected clients containing the player’s name. The name is received on the client and printed to chat.
Do you see how you could adapt this system to your needs?

yes I do and thank you very much

[editline]1st March 2015[/editline]

so this is my final code


hook.Add("PlayerDisconnected", "Spitroast_DisconnectNotice", function( ply )
	PrintMessage( HUD_PRINTTALK, ply:Name().. " has left the server his steam id is".. ply:SteamID() )
end)

hook.Add("PlayerConnect", "Spitroast_ConnectNotice", function( name, ip )
	PrintMessage( HUD_PRINTTALK, name.. " has joined the game." )
end)

if SERVER then -- If we're on the server
	util.AddNetworkString("Example1") -- You must call this on the name of the net message before we can send it!

	hook.Add("PlayerInitialSpawn", "NetLibraryTest_PlayerInitialSpawn", function(ply) -- Add a hook to player initially spawning
		net.Start("Example1") -- begin our net message
			net.WriteString(ply:Nick().. "has joined the server") -- write the player's name, which is a string
		net.Broadcast() -- send the message to all connected players
	end)
else -- We're on client
	net.Receive("Example1", function(len) -- Set up a receiver for the message
		local str = net.ReadString() -- read the string we were sent
		chat.AddText(str) -- Add the str to the chat
	end)
end

You are not grasping the concepts that he is trying to teach you, you merely copied both code examples into one. You need to put some effort into actually reading and trying to understand the code to learn anything.

The first example with hooks works fine but the text will be blue and you cannot change that. The second example does a similar thing for player connecting except it sends it from Server->Client and then the client uses chat.AddText to print the message, that one you can change colors if you follow the example on the Wiki

I know, I am just struggling

[editline]1st March 2015[/editline]

im going to add some stuff to it, im starting to understand it a little better

I’m surprised nobody has suggested game-events yet. They’re SHARED so you can put them in lua/autorun/ or addons/x/lua/autorun/ and they’ll run on client and server.

player_connect and player_disconnect should provide everything you need; I have examples ( also posted on wiki ) which show every value of data that you have access to and what each element means. You can simply add if ( CLIENT ) then chat.AddText( color_white, “…” ); else MsgC( color_white, … … "
" ); end to add output to client and server in the shared file…

https://dl.dropboxusercontent.com/u/26074909/tutoring/game_events/game_event_listeners.lua.html