Net. Usage

I am horribly confused, I tried examples at both wikis but couldnt understand it/get it to work

This is my sending code



KickButton.DoClick = function ()
	net.Start( "myMessage" )
	net.SendToServer()
end


this is my serverside code



util.AddNetworkString( "myMessage" )

net.Receive( "myMessage", function( length, client )
        print( "I got it!" )
end)


if this helps at all

[lua]
if SERVER then

util.AddNetworkString(“Hello”)

net.Receive(“Hello”, function(l, p) – Length of msg, player sending
print(“hi”)
end)

else

net.Start(“Hello”)
net.WriteString(“yo”)
Net.SendToServer()

end[/lua]

The code you provided looks sound. net messages do not require data / contents to be sent. I sent empty messages as “flags” from time to time, this is one of the primary purposes which also allows you to get away from using concommands on the clientside to communicate with the server.

Do you mind showing up your file structure? How did you include the files?

Here’s a quick file just showing different ways to include data such as what needs to be done in order for the client to receive the file AddCSLuaFile via server before the client can include the file. Likewise the server will need to include it as well: https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua

I updated to show what 3 basic files should look like at the root of your gamemode directory in GamemodeName/gamemode/

Is not a gamemode. For educetinal perpuses i am making a cheating script. I am trying to make a button so client tells server to run command kick (nevermind how i got the player is complicated just trying to figure out net library usage first)

This is the code in one file exactly how it is



KickButton.DoClick = function ()
		if ( SERVER ) then
			util.AddNetworkString( "myMessage" )
		end
		net.Start( "myMessage" )
		net.Send( pl )
		
		timer.Simple( 3, function()
			net.Receive( "myMessage", function( length, client )
			print( "I got it!" )
			end )
		end)


Are you trying to run all of that when the button is clicked. That’s what it looks like you are doing.

So, the issue is that you’re using util.AddNetworkString is what appears to be a CLIENT file.

vgui is client-side. Declare the net string in a proper server file.

next, net.Start is fine, net.Send is a server command. The client has the command to SendToServer, use that.

Then, on the server, where you declared the network string, add the net receiver where you do something.

Here’s a TTT script I give out. It should help you understand networking a bit better; I’ll explain it below.
https://dl.dropboxusercontent.com/u/26074909/acecool_ttt_end_round.rar

So Trouble In Terrorist Town has a few SERVER side hooks. Ignore those, and just imagine all the stuff in sv_ is server side as it is. the hook “TTTEndRound” on the sv file is what starts the entire process.

Ignore that it’s wrapped in if ( SERVER ), it’s not necessary but there to make it more clear for people that put it in the wrong area.

So, you see the network string on line 28, and from 46 to 48 is where it sends a message to every player on the server using Broadcast. The message it sends is a simple string. There are more efficient ways to do this, but for learning it’s fine.

on the cl_ file, you’ll see the network receiver for the network string declared on line 28 and sent on line 48 of the sv file.

it reads the string which was sent ( note that all net stuff needs to be read in order, otherwise you’ll improperly cast a variable ) and then it plays the sound.

What you’re trying to do is similar. Instead of the server initializing it via hook, you initialize 46-48 via button press and instead of Broadcast, you need SendToServer. You may not need the write string on 47 unless you target a user, so some data may be required.

the net receiver on the cl file, is pretty much exactly what you need to put on the server-side receiving the message. You may want to change the message name, and obviously the contents aside from read string unless you keep it.

You can set up a chain of these messages.

Try this: on click, SendToServer a message, when the server net.Receive’s that message, net.Broadcast the same message from the server. On the client, receive the message, and send the same message via SendToServer with a string reading final. Have your net.Receive on the server constantly send Broadcast until final is received. Have the client send final to the server.

Experiment with that. Edit the file and remove the write string from the client side, and you’ll see that the server will spam the client, it’s because these can be set up in recursive loops; not advisable in a real environment unless you know what you’re doing, have a time in between and use it as a ping or something.