VGUI and Server

I’m pretty new to the whole coding aspect of things, so I’m trying to get used to some things.

I’ve glanced around the wiki and the LUA command searcher thing looking for an answer about VGUI. I also glanced over the Fretta game mode looking for an answer, but it’s a bit messy to somebody with no experience.

I’m trying to create a VGUI when the player connects to the server and select what weapons he’d like to use. I understand that VGUI is client side. When I try to call commands from DoClick() like Give(), I get a nil error.

I tried looking at:

However I don’t quite understand it. It seems a bit messy and I feel like theres an easier way.

Also, I looked over:

I get an odd error with this one about trying to call some odd datatype and getting nil.

I don’t really need anybody to write it for me. I’m just wondering if anybody could point me in the right direction as to how to go about doing this?

Thank you for your time.

You cant call like give() command from the client, You have to create a serverside command and then run it from the client.

Give is serverside.

This is my function inside init.lua



function GiveWeapon( ply )
	ply:Give( "whatever_weapon" )
end


and in my cl_init.lua I call the function from init.lua:



local ply = LocalPlayer()
GiveWeapon( ply )


Which gives me:


init.lua:25: attempt to call method 'Give' (a nil value)

Oh, That is not how you create console commands.
This is how you create a console command.
[lua]
concommand.Add( “GiveWep1”, function( ply )
ply:Give( “whatever_weapon” )
end )
[/lua]

And then you make the client run the command: “GiveWep1”

That also gives me the same nil value.

Also, I didn’t want to use console commands because new players that join can’t use them to get their weapons unless sv_cheats 1 is enabled, which I don’t really want to have to do.
I also can’t understand why it returns ‘nil’ when I use the function I wrote earlier.

When I do print states and type checks, things like GetPos() and type(ply) get my position and return the type as player. However, it won’t let me do Give(), and I’m in the server files now.

It should work… are you sure you’re calling from clientside and got the command on serverside?

If by that you mean the concommand is in init.lua and I’m calling it from my vgui, then yes.



--Server
local function Give( pl )
  pl:Give( "weapon_whatever" )
end

concommand.Add( "givememystuff", Give )

--Client
button:SetConsoleCommand( "givememystuff" )


No need to override DoClick if all you are going to do is run a console command.

When you click the button, it runs the console command “givememystuff,” which runs the callback on the server and gives the player their weapon.

Alright, this is working for me. Thank you very much.