Run a serverside lua to open a vgui for client

Hey, as the title says I am trying to open a vgui menu when you “Use” on an entity.
I’ve read alot of threads but I didn’t really understand what to do. Could you please explain VERY carefully how to do this?

Let’s say I have this entity, with use function, when he use it, it should open an options menu about this entity and you should be able to edit stuff in this menu.
Like deposit/withdraw money.


On the init.lua (server side)

function ENT:Use(ply, nott)
	umsg.Start( "Unique String", ply )

On the cl_init.lua (client side)

function ClientSideMenu()
	local Derma = Derma derma derma...
usermessage.Hook("Unique String", ClientSideMenu)

Replace “Unique String” with something unique, but it must be the same on both sides.

Send a simple net message to the client on Accept Input.

[lua]// SERVER
util.AddNetworkString( “TestNetworkingEntitiesToClient” );

Server side of ent file: init.lua
[lua]function ENT:AcceptInput( Name, Activator, Caller )
if Name == “Use” and Caller:IsPlayer( ) then
net.Start( “TestNetworkingEntitiesToClient” );
// You can write data if you want, but it’s not necessary unless you have different npcs / different functions where you want to use just one base entity with multiple shots/etc
net.Send( Caller );

Client / receiver
[lua]net.Receive( “TestNetworkingEntitiesToClient”, function( len )
// Do something
local _frame = vgui.Create( “DFrame” );
_frame:SetSize( 500, 500 );
_frame:Center( );
_frame:SetVisible( true );
_frame:MakePopup( );
end )[/lua]

[editline]8th March 2014[/editline]

Ninja’ed; although I’d recommend using net-messages as they’re sent instantly while umsgs are sent the next frame. You could use ENT:Use or ENT:AcceptInput

For something like this, umsg would work just fine. Just some information which can takes its time to get to the client to tell it to open a menu, however, with something more important, especially integers and strings that need to be sent quickly, use ace’s method.

If you** really **want speed for an entity vgui, use a client-side check in either ENT:Draw or ENT:Think that sees if you press the use key while looking at it.

Thanks guys! :slight_smile: Will see what I can do :smiley:

Can you trigger something from clientside to serverside? I mean, when you click a button it should run a function via serverside. Is that possible?

Edit 2:
Okay I managed to run a function with console command but whenever I use something like “self” it just gives me this error:

attempt to call global 'self' (a nil value)

Why would it do that when it’s in the init.lua file?

If it’s not within a SWEP:FunctionName() function then the self variable will not be passed implicitly. I believe you must then use SWEP.

It’s an entity. For darkrp, you’re supposed to put money in a vault then with a derma menu you should be able to take some money out, but I can’t change the value in the money vault from clientside I guess as the “local amount” is in init.lua.