net.WriteEntity returns nil

[lua]net.Receive( “art_Build”, function( len )
local ent = net.ReadEntity();
local class = net.ReadString();

  ent.Building = class;           
  print( "Ent: " .. tostring( ent ) );

end );[/lua]


[lua]function ENT:Initialize()
self.Building = “human_resourcer”;
net.Start( “art_Build” );
net.WriteEntity( self );
net.WriteString( self.Building );

Now I get printed

ENT: [NULL Entity]

Is this broken orrrr is something interfering?

The entity might not be in the same visleaf as the player so it isn’t valid.

Is there a fix or a work-around?

You can either use DT variables or set the entity’s transmit state to transmit regardless of PVS. I suggest using DT variables. They’re like NetworkedVars except not shitty. They’re sent with the other stuff like velocity, health, and position.

self:SetDTString(i, “hi”)
local something = self:GetDTString(i)

Where i is an integer from 0 to 3. You can set or get it on the client or server. It’s only sent when it changes.

Thank you that worked just fine

I just want to add that I had the same/very similar issue, net.ReadEntity returning a NULL entity clientside. The issue was I was doing it as the player was spawning. By changing it to write/readInt, and using Entity(int), and using a timer to check if the entity was NULL, about 0.3 seconds later the entity is valid.