Net messages and NULL entities.

Hello,

I’m trying to make a friendlist. So far, I’ve got this code.

In cl_init.lua



function Utilities()

	local frame = vgui.Create( "DFrame" )
	frame:SetPos( ScrW() * 0.25, ScrH() * 0.25 ) 
	frame:SetSize( ScrW()/2, ScrH()/2 ) 
	frame:SetTitle( "Utilities" )
	frame:SetVisible( true )
	frame:SetDraggable( true )
	frame:ShowCloseButton( true )
	frame:MakePopup()
	
		playerframe = vgui.Create( "DCollapsibleCategory", frame )    
		playerframe:SetPos( 25, 50 )                                            
		playerframe:SetSize( frame:GetWide()/4 , frame:GetTall()/3 )                      
		playerframe:SetExpanded( 0 )                                           
		playerframe:SetLabel( "Players" )
		 
		playerlist = vgui.Create( "DPanelList", playerframe )  
		playerlist:SetPos( 0, 30 )                     
		playerlist:SetSize( playerframe:GetWide(), playerframe:GetTall()*2.3 )                
		playerlist:SetSpacing( 5 )                   
		playerlist:EnableHorizontal( false )           
		playerlist:EnableVerticalScrollbar( true )   

		for k, v in pairs( player.GetAll() ) do
		
			if ClientFriendTable == nil or table.HasValue( ClientFriendTable, v ) == false then
			--print( "Player", v:Nick(), "is in clientside friend table?", table.HasValue( ClientFriendTable, v ) )
					playerbutton = vgui.Create( "DButton", frame )
					playerbutton:SetPos( frame:GetTall() * 0.1, 30 ) --Place it half way on the tall and 5 units in horizontal
					playerbutton:SetSize( 200, 40 )
					playerbutton:SetText( v:Nick() )
					playerlist:AddItem( playerbutton )
					playerbutton.DoClick = function()
					
					timer.Simple(0.1, function()
					
						net.Start( "FriendToAdd" )
			
							net.WriteEntity( v )
			
						net.SendToServer() 
						
					end)
					
						RunConsoleCommand( "addFriend" )
							
					end
			
			end	
		
		end
		
		friendframe = vgui.Create( "DCollapsibleCategory", frame )    
		friendframe:SetPos( 25 + frame:GetWide() * 1/4, 50 )                                             
		friendframe:SetSize( frame:GetWide()/4 , frame:GetTall()/3 )                                      
		friendframe:SetExpanded( 0 )                                         
		friendframe:SetLabel( "Friends" )
		 
		friendlist = vgui.Create( "DPanelList", friendframe )  
		friendlist:SetPos( 0, 30 )                         
		friendlist:SetSize( friendframe:GetWide(), friendframe:GetTall()*2.3 )    
		friendlist:SetSpacing( 5 )                    
		friendlist:EnableHorizontal( false )            
		friendlist:EnableVerticalScrollbar( true )

		for k, v in pairs( player.GetAll() ) do

			if ClientFriendTable != nil and table.HasValue( ClientFriendTable, v ) == true then
					
					friendbutton = vgui.Create( "DButton", frame )
					friendbutton:SetPos( frame:GetTall() * 0.1, 30 ) 
					friendbutton:SetSize( 200, 40 )
					friendbutton:SetText( v:Nick() )
					friendlist:AddItem( friendbutton )
					friendbutton.DoClick = function()
					
					
					timer.Simple(0.1, function()
					
						net.Start( "FriendToRemove" )
			
							net.WriteEntity( v )
			
						net.SendToServer() 
					
					end)
						
						RunConsoleCommand( "addFriend" )
							
					end
			
			end	
		
		end
		
end
concommand.Add( "utilities", Utilities )


And then in init.lua



net.Receive( "FriendToAdd", function( length, client )
   
		print( "player", net.ReadEntity():Nick(), "received, is valid?", net.ReadEntity():IsValid() )
		table.insert( client.FriendTable, net.ReadEntity() )
	
	print( "serverside friendlist is" )
	PrintTable( client.FriendTable )
		
end )

net.Receive( "FriendToRemove", function( length, client )
   
	if net.ReadEntity() and net.ReadEntity():IsValid() then
		
		table.RemoveByValue( client.FriendTable, net.ReadEntity() )
		
	end
	
	print( "serverside friendlist is" )
	PrintTable( client.FriendTable )
		
end )

function AddFriend( ply )

	timer.Simple(0.1, function()

		net.Start( "FriendToClient" ) 
			
			net.WriteTable( ply.FriendTable )
				
		net.Send( ply )
		
	end)

end
concommand.Add( "addFriend", AddFriend )


This system should work by creating a list of all players in the server. All those who are in the friend table appear in the friends list, and those who aren’t appear in the players list. When the player clicks on a player in the player list, they should be added to the friend table serverside, and then updated clientside. Vice versa for players already in the friend list.

The problem is that the console always prints that the player is a NULL entity, so the system doesn’t work. I tried putting in some timers, since I heard that net.readentity was a bit buggy and could lag sometimes, but no luck so far. Any ideas?

Show the rest of client side
Also, have you tried adding a check if entity:IsValid()

Oh, it’s the con command, AddFriend wants an entity, I doubt that you are entering the entity into console, you are probably entering the entities name.
Actually, you’re not even giving an argument for the con command in the client side code.

You’re calling net.ReadEntity too much
Store the player as a variable

[LUA]
local ply = net.ReadEntity()
print( ply:IsValid(), net.ReadEntity() )
[/LUA]

also localize your variables

I think I solved it actually. I let the client determine the contents of their friend list as a table, and then just sent it to the server. This seems to work absolutely fine.