*Help* Debugging net.Receive!

hi guys I have been working with some net functions from server side functions to the client side. But I have come across a small issue.

So when a player joins initially they are meant to receive a graphic which is placed on their screen. But it seems to only work when they reconnect after being connected in the first place.
Here is my code.



'Server Side'
if ply:Team() ~= Contenders and math.random(1,1) == 1 then --Contender
	 ply:SetTeam( 1 ) 
timer.Simple(8, function()	 
	util.AddNetworkString( "contender" ) --Send a network message for the contender team
	 net.Start("contender")
     net.WriteString("some text")
    net.Send(ply)
	end)
	 end




'Client Side'
local class2 = {
		duration 	= 5; -- Length of time to display image
		color 		= Color( 225, 225, 225, 400 ); -- Color overlay of image; white = original color of image
		material 	= Material( "class2.png" ); -- Material to be used
		x 			= 800; -- x coordinate for the material to be rendered ( mat is drawn from top left to bottom right )
		y 			= 700; -- y coordinate for the material to be rendered ( mat is drawn from top left to bottom right )
		w 			= 500; -- width of the material to span
		h 			= 350; -- height of the material to span
	};	




  net.Receive( "contender", function( len, pl ) 
  --timer.Simple(8, function()
		--// Reset, just in case it was set somewhere
		LocalPlayer( ).__DisplaySomethingTimer = nil;

	--	// Add a HUDPaint hook when the DisplaySomething net message is received
		hook.Add( "HUDPaint", "DisplaySomething:HUDPaint", function( )
		--	// Helper Variable
			local _p = LocalPlayer( );

		--	// Wait till player is valid to itself
			if ( !IsValid( _p ) ) then return; end

		--	// Initialize the timer if it hasn't already been initialized
			if ( !_p.__DisplaySomethingTimer ) then _p.__DisplaySomethingTimer = CurTime( ); end

			--// Deal with the length and convert to 0-1 fraction/float
			 local _fraction = math.Clamp( math.TimeFraction( _p.__DisplaySomethingTimer, _p.__DisplaySomethingTimer + DisplaySomethingConfig.duration, CurTime( ) ), 0, 1 );

			--// If that doesn't work, do it the old-fashioned way :-)
			--local _fraction = CurTime( ) - _p.__DisplaySomethingTimer;
			
			--// Remove the Something it when the time is right - It's clamped so it'll never be < 0, however if you ever do something where you don't clamp, and it can be out of bounds, use a < or > to ensure code is executed.
			if ( _fraction >= class2.duration ) then
				hook.Remove( "HUDPaint", "DisplaySomething:HUDPaint" );
				_p.__DisplaySomethingTimer = nil;
			end

			surface.SetMaterial( class2.material );-- // Set the material
			surface.SetDrawColor( class2.color );-- // White ensures normal color
			surface.DrawTexturedRect( class2.x, class2.y, class2.w, class2.h ); --// Draw the textured Rect
			
			--// Display Something...
			surface.SetMaterial( class2.material );-- // Set the material
			surface.SetDrawColor( class2.color ); --// White ensures normal color
			
			surface.DrawTexturedRect( class2.x, class2.y, class2.w, class2.h ); --// Draw the textured Rect
			
			return false 
	end)
end ) 	



I know its a lot to look through but its mainly just the same thing over and over. I hope someone can help me with this. Otherwise it is quite a good example for a new person who needs help with net.Receive to use as a reference I guess.

Many thanks.

-Duby



if ply:Team() ~= Contenders and math.random(1,1) == 1 then --Contender
     ply:SetTeam( 1 ) 
timer.Simple(8, function()     
    util.AddNetworkString( "contender" ) --Send a network message for the contender team
     net.Start("contender")
     net.WriteString("some text")
    net.Send(ply)
    end)
     end


You need to call util.AddNetworkString only once per message, and not when you are sending a message. Put it outside of the if statement at the top of the file.

It caches the name of the net message for all future use, but takes some amount of time before it can actually be used. That’s probably why the first player to connect to the server won’t see the message until they rejoin, because the string wasn’t cached by the time it tried sending it.

The main issue which I didn’t also specify is that I have about 7 teams. So they would all mess with each other.

Here is a system that displays something on screen when a player joins for x seconds. https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/display_something_on_initial_spawn.lua.html

Edit: Oh, you did post my code up at the top, I thought it looked familiar.

Edit: So you want to edit it so that different teams get different images or what? Well, when a player first joins, the team wouldn’t typically be set unless you have a system in place that does it. Just grab player:Team( ) on client-side and map it to a material in the config table, it would be the easiest way

Your code? I got some help from someone else about 3 months ago for a different function. I have use changed it slightly to allow me to do this. But yes each team has a different image. It basically tells them which team they are on.

I have set it so that when they join the team they are set is randomised. As you can see on the server side code. But the issue is that this code only wants yo work when you reconnect o the server. The server side is within the init.lua and the client side is within the cl_init.lua . So that may help you understand. Just imagine that code but multiplied 7 times for other teams.

Thanks

Duby

Why is util.AddNetworkString() inside a timer? You’re supposed to add that at the top of your files as it is meant to precache the string for networking. Try doing that and see if anything changes; I’m assuming you’re testing this on your client at the moment correct?

Also, why do you have seven teams? I’m sure having different net messages for different teams won’t mess them up as long as you have different names for them.

Yes, my code. Here’s your original post where I wrote this up: http://forum.facepunch.com/showthread.php?t=1378165&p=44309096&viewfull=1#post44309096

I’ll be happy to help you adapt it to your needs…

Here: https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/display_something_on_initial_spawn_teams.lua.html Remove .html to see the .lua code.

Add teams and the materials…

I see, so I should run this from the auto run server and client folder file? Rather than the init.lua and the cl_init.lua?

You can put it where you want. If you use that file as-is then put either in: lua/autorun/blah.lua
or: addons/<addon>/lua/autorun/blah.lua

or in shared file on your server.

If you split them up, put server in addons/<addon>/lua/autorun/server/blah.lua or lua/autorun/server/blah.lua
and client: addons/<addon>/lua/autorun/client/blah.lua or lua/autorun/client/blah.lua

Or in client/server in cl_init.lua and init.lua or where-ever else you have client/server code

Hmm ok this makes some sense. I will give it a go when I am home from college and then respond to this thread if I am having anymore issues.

Thanks for the in-depth help.

Sorry but I have been ill for the past few days. Right I actually don’t understand the method you are talking about as I didn’t get my other method working the same way so did yours. Let me paste up my code for client side and serverside so you can understand what I am doing. Bare in mind I have about 6 teams.




Init.lua

function GM:PlayerInitialSpawn( ply )--For when the player spawns for the first time.
ply:SetTeam( 1 )

if ply:Team() ~= Contenders and math.random(1,1) == 1 then --Contender
	ply:SetTeam( 1 ) 
	timer.Simple(8, function()	 
	util.AddNetworkString( "contender" ) --Send a network message for the contender team
	net.Start("contender")
    net.WriteString("some text")
    net.Send(ply)
	end)
	 end
		if ply:Team() ~= Contenders and math.random(1,5) == 1 then --Mr.sLayer
	ply:SetTeam( 11 )
	timer.Simple(8, function()
	util.AddNetworkString( "slayer" ) --Send a network message for Mr.sLayer
	net.Start("slayer")
    net.WriteString("some text")
   net.Send(ply)
	 end)
	end

end







cl_init.lua

	
local class2 = {
		duration 	= 5; -- Length of time to display image
		color 		= Color( 225, 225, 225, 400 ); -- Color overlay of image; white = original color of image
		material 	= Material( "class2.png" ); -- Material to be used
		x 			= 800; -- x coordinate for the material to be rendered ( mat is drawn from top left to bottom right )
		y 			= 700; -- y coordinate for the material to be rendered ( mat is drawn from top left to bottom right )
		w 			= 500; -- width of the material to span
		h 			= 350; -- height of the material to span
	};	


	
local class6 = {
		duration 	= 5; -- Length of time to display image
		color 		= Color( 225, 225, 225, 400 ); -- Color overlay of image; white = original color of image
		material 	= Material( "class6.png" ); -- Material to be used
		x 			= 800; -- x coordinate for the material to be rendered ( mat is drawn from top left to bottom right )
		y 			= 700; -- y coordinate for the material to be rendered ( mat is drawn from top left to bottom right )
		w 			= 500; -- width of the material to span
		h 			= 350; -- height of the material to span
	};		

net.Receive( "contender", function( len, pl ) 
  --timer.Simple(8, function()
		--// Reset, just in case it was set somewhere
		LocalPlayer( ).__DisplaySomethingTimer = nil;

	--	// Add a HUDPaint hook when the DisplaySomething net message is received
		hook.Add( "HUDPaint", "DisplaySomething:HUDPaint", function( )
		--	// Helper Variable
			local _p = LocalPlayer( );

		--	// Wait till player is valid to itself
			if ( !IsValid( _p ) ) then return; end

		--	// Initialize the timer if it hasn't already been initialized
			if ( !_p.__DisplaySomethingTimer ) then _p.__DisplaySomethingTimer = CurTime( ); end

			--// Deal with the length and convert to 0-1 fraction/float
			 local _fraction = math.Clamp( math.TimeFraction( _p.__DisplaySomethingTimer, _p.__DisplaySomethingTimer + DisplaySomethingConfig.duration, CurTime( ) ), 0, 1 );

			--// If that doesn't work, do it the old-fashioned way :-)
			--local _fraction = CurTime( ) - _p.__DisplaySomethingTimer;
			
			--// Remove the Something it when the time is right - It's clamped so it'll never be < 0, however if you ever do something where you don't clamp, and it can be out of bounds, use a < or > to ensure code is executed.
			if ( _fraction >= class2.duration ) then
				hook.Remove( "HUDPaint", "DisplaySomething:HUDPaint" );
				_p.__DisplaySomethingTimer = nil;
			end

			surface.SetMaterial( class2.material );-- // Set the material
			surface.SetDrawColor( class2.color );-- // White ensures normal color
			surface.DrawTexturedRect( class2.x, class2.y, class2.w, class2.h ); --// Draw the textured Rect
			
			--// Display Something...
			surface.SetMaterial( class2.material );-- // Set the material
			surface.SetDrawColor( class2.color ); --// White ensures normal color
			
			surface.DrawTexturedRect( class2.x, class2.y, class2.w, class2.h ); --// Draw the textured Rect
			
			return false 
	end)
end ) 	

net.Receive( "slayer", function( len, pl )
--timer.Simple(8, function()
		--// Reset, just in case it was set somewhere
		LocalPlayer( ).__DisplaySomethingTimer = nil;

	--	// Add a HUDPaint hook when the DisplaySomething net message is received
		hook.Add( "HUDPaint", "DisplaySomething:HUDPaint", function( )
		--	// Helper Variable
			local _p = LocalPlayer( );

		--	// Wait till player is valid to itself
			if ( !IsValid( _p ) ) then return; end

		--	// Initialize the timer if it hasn't already been initialized
			if ( !_p.__DisplaySomethingTimer ) then _p.__DisplaySomethingTimer = CurTime( ); end

			--// Deal with the length and convert to 0-1 fraction/float
			-- local _fraction = math.Clamp( math.TimeFraction( _p.__DisplaySomethingTimer, _p.__DisplaySomethingTimer + DisplaySomethingConfig.duration, CurTime( ) ), 0, 1 );

			--// If that doesn't work, do it the old-fashioned way :-)
			local _fraction = CurTime( ) - _p.__DisplaySomethingTimer;
			
			--// Remove the Something it when the time is right - It's clamped so it'll never be < 0, however if you ever do something where you don't clamp, and it can be out of bounds, use a < or > to ensure code is executed.
			if ( _fraction >= class6.duration ) then
				hook.Remove( "HUDPaint", "DisplaySomething:HUDPaint" );
				_p.__DisplaySomethingTimer = nil;
			end

			surface.SetMaterial( class6.material );-- // Set the material
			surface.SetDrawColor( class6.color );-- // White ensures normal color
			surface.DrawTexturedRect( class6.x, class6.y, class6.w, class6.h ); --// Draw the textured Rect
			
			--// Display Something...
			surface.SetMaterial( class6.material );-- // Set the material
			surface.SetDrawColor( class6.color ); --// White ensures normal color
			
			surface.DrawTexturedRect( class6.x, class6.y, class6.w, class6.h ); --// Draw the textured Rect
			return false 
	end)
end )




#Bump