Join/Disconnect

Trying to make a join/disconnect message which displays name, and steam id. The current code I have doesn’t seem to work. The file location is currently lua/autorun/server



function FirstSpawn( ply )
    PrintMessage( HUD_PRINTTALK, ply:Nick().. " entered the server, their SteamID is: " ..ply:SteamID() )
end
 
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )

function PlayerDisconnect( ply )
    PrintMessage( HUD_PRINTTALK, ply:Nick().. " has disconnected, their SteamID is: " ..ply:SteamID() )
end
 
hook.Add( "PlayerDisconnected", "playerDisconnected", PlayerDisconnect )


Try printing something and see what happens

I’d try changing those id strings, naming them after the hook itself is honestly stupid. If that doesn’t work then try printing something like said above, remember it’ll print to server console so you’ll have to look there.

Also I’d make those functions either local or anonymous as they don’t need to be global

please use


 tags

This is perhaps the best addon I’ve seen that does this: https://github.com/myudev/gmod_addons/tree/advjoinleave

Someone from Ulysses already did this.

I rewrote the code though to ignore the wav noises that came with it and also removed the steam ID. Let me get you the code.
EDIT: I rewrote the Steam ID feature in to print the Steam ID.

Save File As: cl_player.lua


// client side apple
if SERVER then return end

// Connected
function PlayerConnectAnnouncement( data )
local name = data:ReadString()
local isokayforsound = data:ReadString()
	chat.AddText( Color( 255, 0, 255 ), "[Server] ", Color( 255, 255, 255 ), name, " has connected to the server. Their ID is: "..steamid )

end
usermessage.Hook("PlayerConnectAnnouncement", PlayerConnectAnnouncement)

// Disconnect
function PlayerDisconnectAnnouncement( data )
local name = data:ReadString()
local teamcolour = team.GetColor(data:ReadShort())
local steamid = data:ReadString()
local isokayforsound = data:ReadString()
	chat.AddText( Color( 255, 0, 255 ), "[Server] ", teamcolour, name, Color( 255, 255, 255 ), " has left the server. Their ID was: "..steamid )

end
usermessage.Hook("PlayerDisconnectAnnouncement", PlayerDisconnectAnnouncement)

// Disconnect
function PlayerDisconnectAnnouncement2( data )
local name = data:ReadString()
local teamcolour = team.GetColor(data:ReadShort())
local isokayforsound = data:ReadString()
	chat.AddText( Color( 255, 0, 255 ), "[Server] ", teamcolour, name, Color( 255, 255, 255 ), " has left the server." )

end
usermessage.Hook("PlayerDisconnectAnnouncement2", PlayerDisconnectAnnouncement2)


// Error
function PlayerJDAnnouncement( data )
local errrodata = data:ReadString()
	chat.AddText( errrodata )
	MsgC(Color(255,0,0,0), errrodata)
	surface.PlaySound( "garrysmod/save_load3.wav" )
end
usermessage.Hook("PlayerJDAnnouncement", PlayerJDAnnouncement)


local ADDON_NAME = "join_disconnect"
usermessage.Hook(ADDON_NAME, function(data)
local Version1 = data:ReadString()
local Version2 = data:ReadString()
local ADDON_ACTUAL_NAME = data:ReadString()
local DOWNLOAD_LINK = data:ReadString()
local Frame = vgui.Create( "DFrame" )
Frame:SetSize( 300, 150 ) 
Frame:Center()
Frame:SetTitle( "Addon: "..ADDON_ACTUAL_NAME.." needs updating" ) 
Frame:SetSizable(false)
Frame:SetDraggable(false)
Frame:ShowCloseButton( false ) 
Frame:MakePopup() 

local FLabel = vgui.Create( "DLabel", Frame )
FLabel:Center()
FLabel:SetPos( 15, 27 )
--FLabel:SetColor(Color(255,255,255,255)) // Color
--FLabel:SetFont("default")
FLabel:SetText("The addon: "..ADDON_ACTUAL_NAME.." is outdated!")
FLabel:SizeToContents()

local FLabel2 = vgui.Create( "DLabel", Frame )
FLabel2:Center()
FLabel2:SetPos( 15, 40 )
--FLabel2:SetColor(Color(255,255,255,255)) // Color
--FLabel2:SetFont("default")
FLabel2:SetText("Please contact system administrator to update addon!")
FLabel2:SizeToContents()

local FLabel3 = vgui.Create( "DLabel", Frame )
FLabel3:Center()
FLabel3:SetPos( 15, 53 )
--FLabel3:SetColor(Color(255,255,255,255)) // Color
--FLabel3:SetFont("default")
FLabel3:SetText("Server's Version:")
FLabel3:SizeToContents()

local FLabel35 = vgui.Create( "DLabel", Frame )
FLabel35:Center()
FLabel35:SetPos( 100, 53 )
FLabel35:SetColor(Color(255,0,0,255)) // Color
--FLabel35:SetFont("default")
FLabel35:SetText(Version1)
FLabel35:SizeToContents()

local FLabel4 = vgui.Create( "DLabel", Frame )
FLabel4:Center()
FLabel4:SetPos( 15, 66 )
--FLabel4:SetColor(Color(255,255,255,255)) // Color
--FLabel4:SetFont("default")
FLabel4:SetText("Online Version:")
FLabel4:SizeToContents()

local FLabel45 = vgui.Create( "DLabel", Frame )
FLabel45:Center()
FLabel45:SetPos( 90, 66 )
FLabel45:SetColor(Color(255,0,0,255)) // Color
--FLabel45:SetFont("default")
FLabel45:SetText(Version2)
FLabel45:SizeToContents()

local FLabel5 = vgui.Create( "DLabel", Frame )
FLabel5:Center()
FLabel5:SetPos( 15, 81 )
--FLabel5:SetColor(Color(255,255,255,255)) // Color
--FLabel5:SetFont("default")
FLabel5:SetText("Addon:")
FLabel5:SizeToContents()

local FLabel55 = vgui.Create( "DButton", Frame )
FLabel55:SetSize(ScrW() * 0.025, ScrH() * 0.015)
FLabel55:Center()
FLabel55:SetPos( 52, 83 )
FLabel55:SetText("Link")
FLabel55.DoClick = function()
gui.OpenURL(DOWNLOAD_LINK)
end

local Close = vgui.Create("DButton", Frame)
Close:SetSize(ScrW() * 0.050, ScrH() * 0.025)
Close:Center()
Close:SetPos(105,120)
Close:SetText("Ok")
Close.DoClick = function()
if Checkbox:GetChecked() == true then
net.Start( ADDON_NAME )
	net.WriteEntity(LocalPlayer())
net.SendToServer()
end
end

local Checkbox = vgui.Create( "DCheckBox", Frame )
Checkbox:Center()
Checkbox:SetPos( 65, 105 )
Checkbox:SetChecked( false )

local FLabel6 = vgui.Create( "DLabel", Frame )
FLabel6:Center()
FLabel6:SetPos( 85, 105 )
FLabel6:SetColor(Color(255,0,0,255)) // Color
FLabel6:SetFont("default")
FLabel6:SetText("Never show message again!")
FLabel6:SizeToContents()

local Close = vgui.Create("DButton", Frame)
Close:SetSize(ScrW() * 0.050, ScrH() * 0.025)
Close:Center()
Close:SetPos(105,120)
Close:SetText("Ok")
Close.DoClick = function()
if Checkbox:GetChecked() == true then
net.Start( ADDON_NAME )
	net.WriteEntity(LocalPlayer())
net.SendToServer()
end
	Frame:Close()
end
end)

Save File As: sv_player.lua


// server side apple
if CLIENT then return end
AddCSLuaFile( "cl_player.lua"  )

if file.Exists( "apple_join_disconnect_warning/indicate.txt", "DATA" ) == false then
	file.CreateDir("apple_join_disconnect_warning")
	file.Write( "apple_join_disconnect_warning/indicate.txt", "1" )
end


//Connected
function PlayerConnectAnnouncement( ply, ip )
	for k,v in pairs(player.GetAll()) do
		umsg.Start( "PlayerConnectAnnouncement", v)
			umsg.String(ply)
			umsg.String(file.Read( "apple_join_disconnect_warning/indicate.txt" ))
		umsg.End()
	end
Msg("Player " .. ply .. " has connected to the server.
")
end
hook.Add( "PlayerConnect", "PlayerConnectAnnouncement", PlayerConnectAnnouncement )

//Spawn
function PlayerInitialSpawnAnnouncement( ply )
	timer.Simple( 3, function()
	if !ply:IsValid() then return end
	for k,v in pairs(player.GetAll()) do
		if v:IsAdmin() then
		--	if ply:IsBot() then return end
			umsg.Start( "PlayerInitialSpawnAnnouncement", v)
				umsg.String(ply:Nick())
				umsg.Short(ply:Team())
				umsg.String(ply:SteamID())
				umsg.String(file.Read( "apple_join_disconnect_warning/indicate.txt" ))
			umsg.End()
			else
			umsg.Start( "PlayerInitialSpawnAnnouncement2", v)
				umsg.String(ply:Nick())
				umsg.Short(ply:Team())
				umsg.String(file.Read( "apple_join_disconnect_warning/indicate.txt" ))
			umsg.End()
		end
	end
Msg("Player " .. ply:Nick() .. " has spawned in the server.
")
end)
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawnAnnouncement", PlayerInitialSpawnAnnouncement )

 
//Disconnect
function PlayerDisconnectAnnouncement( ply )
	for k,v in pairs(player.GetAll()) do
		if v:IsAdmin() then
			if ply:IsBot() then return end
			umsg.Start( "PlayerDisconnectAnnouncement", v)
			umsg.String(ply:Nick())
			umsg.Short(ply:Team())
			umsg.String(ply:SteamID())
			umsg.String(file.Read( "apple_join_disconnect_warning/indicate.txt" ))
			umsg.End()
		else
			umsg.Start( "PlayerDisconnectAnnouncement2", v)
			umsg.String(ply:Nick())
			umsg.Short(ply:Team())
			umsg.String(file.Read( "apple_join_disconnect_warning/indicate.txt" ))
			umsg.End()
		end
	end
Msg("Player " .. ply:Nick() .. " has left the server.
")
end
hook.Add( "PlayerDisconnected", "PlayerDisconnectAnnouncement", PlayerDisconnectAnnouncement )



concommand.Add( "jd_sounds", function(ply, cmd, args)
	if ply:IsPlayer() == false || ply:IsSuperAdmin() == false then 
		MsgC(Color(255,0,0,255),"Either console has attempted to turn sounds off, or a player is attempting to turn sounds off for apple_join_disonnect_warning addon!")
	return end
	if tostring(args[1]) == "on" then
		file.Write( "apple_join_disconnect_warning/indicate.txt", "1" )
		umsg.Start( "PlayerJDAnnouncement", ply)
			umsg.String("You have turned on sounds for: Apples_Join_Disconnect_Warning")
		umsg.End()
	elseif tostring(args[1]) == "off" then
		file.Write( "apple_join_disconnect_warning/indicate.txt", "0" )
		umsg.Start( "PlayerJDAnnouncement", ply)
			umsg.String("You have turned off sounds for: Apples_Join_Disconnect_Warning")
		umsg.End()
	else
		umsg.Start( "PlayerJDAnnouncement", ply)
			umsg.String("There are only two options, on or off. You selected '"..tostring(args[1]).."' which is not a valid option!")
		umsg.End()
	end
end)





if SERVER then
local YOUR_VERSION = "1.11"
local PLY2 = "105"
local ADDON_NAME = "join_disconnect"
local ADDON_ACTUAL_NAME = "apple_join_disconnect_warning"
local DOWNLOAD_LINK = "http://goo.gl/VitT7Z"
local MESSAGE_TO_SERVER = "APPLE'S JOIN/DISCONNECT MESSAGE"

hook.Add('PlayerInitialSpawn','PlayerInitialSpawn'..ADDON_NAME, function(ply)
http.Fetch( "https://raw.githubusercontent.com/chaos12135/gmod-development/master/branches/"..ADDON_NAME..".txt", function( body, len, headers, code )
if body == nil then return end
local body = string.Explode(" ",body)
	if tostring(body[1]) != tostring("Version") then return end
	if tonumber(body[2]) != tonumber(YOUR_VERSION) then
		if ply:GetPData(ADDON_NAME..""..PLY2) != nil || ply:GetPData(ADDON_NAME..""..PLY2) != "1" && ply:IsSuperAdmin() == true then
			umsg.Start(ADDON_NAME, ply)
				umsg.String(YOUR_VERSION)
				umsg.String(body[2])
				umsg.String(ADDON_ACTUAL_NAME)
				umsg.String(DOWNLOAD_LINK)
			umsg.End()
		end
		
		MsgC("
",Color(255,0,0,255),"- - OUT OF DATE - -","
")
		MsgN("~"..MESSAGE_TO_SERVER.."~")
		MsgC(Color(255,255,255,255),"Your Version: ",Color(255,0,0,255),YOUR_VERSION,"
")
		MsgC(Color(255,255,255,255),"Online Version: ",Color(255,0,0,255),body[2])
		MsgN("We here at Apple Inc. strongly suggest that you keep this addon updated")
		MsgC(Color(255,255,255,255),"Please go here and update: ",Color(0,255,255,255),""..DOWNLOAD_LINK.."
")
	end
end, 
function( error )
	MsgN("DOESNOT WORK")
end)
end)
net.Receive( ADDON_NAME, function( length, client )
	net.ReadEntity():SetPData(ADDON_NAME..""..PLY2,"1")
end )
util.AddNetworkString( ADDON_NAME )
end

EDIT:
I ALMOST FORGOT!!! D:
Save To: garrysmod/addons
Create a Folder: (WHATEVER YOU WANT)
Then reload the server.