Net Menu Help

Hello. I’m very inexperienced with the net menu and my current methods(console commands and NWBools) to communicate with the client from the server take up a lot of data, so I’ve heard. Can someone explain to me/teach me how I’d use the net menu to do the following?

-Send a net message from the server to the client that gives the local player a weapon(just use weapon_smg1 for an example)

Call me lazy or dumb, but tutorials haven’t helped me. I’ve looked on the wiki and all that but the net library still puzzles me…

Client:



local button = vgui.Create( "DButton" );
button.DoClick = function()
    net.Start( "givewepon" );
    net.SendToServer();
end;


Server:



util.AddNetworkString( "givewepon" );
net.Receive( "givewepon", function( ply, len )
    ply:Give( "weapon_smg1" );
end );


you cant send a net message to client, that would mean clients could give themself weapons

Thanks! The help is really appreciated.

To open a menu:

[lua]
– Server
util.AddNetworkString( “OpenMenuUniqueName” )

function MakeClientOpenMenu( Pl )

net.Start( “OpenMenuUniqueName” )
net.Send( Pl )

end

– Client

net.Receive( “OpenMenuUniqueName”, function()
– Menu code
end)
[/lua]

To give a player a weapon, you do that serverside, so in an OnClick method for one of your vgui elements (that gives a player a weapon), send a net to the server (net.SendToServer), do your validation code (to make sure players can’t abuse it), and give the weapon (net.ReadString maybe)

[editline]23rd May 2015[/editline]

Never been ninja’d by 2 posts before

?


[ERROR] addons/csscontent/gamemodes/zs/gamemode/init.lua:9: attempt to index local 'ply' (a number value)
  1. func - addons/csscontent/gamemodes/zs/gamemode/init.lua:9
   2. unknown - lua/includes/modules/net.lua:32


With that code, anyone can give themselves a weapon by executing lua clientside. Make sure to check that the person who sent the net message is actually supposed to be able to do it. For example, if it’s an admin menu, make sure that the player is an admin before executing the rest of the net.receive callback. As the saying goes, never trust the client. Also, your order of arguments in that net.receive callback are wrong: the length is first, then the player.

As for the OP, the player:Give method is serverside, not clientside. That’s why you need to send it from the client to the server, not the other way around. Obviously, if you need to call player:Give in serverside code, then don’t network it. It’s a good idea to only network things when it is necessary.

I see you’re responding to the question I asked on Problems that don’t need their own thread about a menu with net messages :wink:

[editline]23rd May 2015[/editline]

I’m not making an admin menu. Anyways, so how would I stop someone from using the console to get the weapon? Actually, wait, is lua_run_cl allowed for everyone on, lets say, a server? or is it by default allowed by everyone?

It’s only allowed if sv_allowcslua is set to 0, however there are ways that I won’t mentioned that can be used to execute code without sv_allowcslua set to 1. You could, for example, create a window that times out after 30 seconds from when the net message was sent and only allow a response within those 30 seconds, while restricting the player to only making 1 response per query.