Give Player Item via VGui

Hello, i have made a Vgui wich can be open by a Entity i added a Button which should give the Player a specifig weapon but i don’t know how i say the Server to give the Player the Weapon.

1 Like

The easiest way would be with a custom console command, though, you do need to check on the server if the player is allowed to get that weapon and make sure they give themselves any unauthorized weapons or spawn random entities.

On the server you would register the command with an allowed weapon list like so:

-- Allowed items that players can get.
local AllowedWeapons = {
    ["weapon_crossbow"] = true,
    ["weapon_crowbar"] = true
}

concommand.Add("give_me_weapon", function( ply, cmd, args )
    local WeaponName = args[1]

    -- Check if we are allowed to spawn this weapon.
    if AllowedWeapons[WeaponName] then
        ply:Give( WeaponName )
    end
end)

On the client you would then simply run the following to get a weapon:

RunConsoleCommand("give_me_weapon", "weapon_name_here")

For a better and cleaner approach you could look into using net messages, the advantage would be that players can’t enter the command straight into their console, though net messages could still be manually called with exploits, so always check on the server if the player is allowed to get a certain weapon. Either through a simple list or other means.

Hope this helps!

2 Likes

Not sure if that’s a great idea, it’s hard to understand but I’m guessing what he’s talking about is some kind of vending machine that opens a shop.
Having it tied to a command would mean players could just run it in their console wherever and whenever, to fix that you’d have to put checks in place.

Better stick with net messages, @NupsNils here’s a quick rundown since the wiki doesn’t have good examples:

I’m guessing OP already has it set up with a Button:DoClick(), since it only runs on client.

On the server’s side you’ll need to add a networked variable (Make sure the name is unique):

util.AddNetworkString("WeaponRequest")

Then, on the client’s side, in the Button:DoClick() function you put the following:

net.Start("WeaponRequest")
	net.WriteString(nameOfWeapon) --Make sure it's a string variable, like "weapon_crowbar"
net.SendToServer()

Back to the server, we need to instruct what to do when we receive the networked message:

net.Receive("WeaponRequest", function(len, ply)

	local weapon = net.ReadString()

	if(ply && ply:IsValid() && ply:Alive()) then --We make sure to check so it doesn't throw us a lua error

		ply:Give(weapon)

	else

		print("Invalid player")

	end

end)
3 Likes

HOLY SHIT i… i… is that the real bumpy 0-0 the one who did all them things like that one thing and that other thing… 0-0

but yeah, try use the net messages stuff if you can, since you ideally dont want to be using console commands for stuff, unless its something like causing player animations, or things like that which just changes how stuff looks client side but does not effect gameplay, unless its something like equipping something you have already but quicker and such.
but if its like spawning money in something, weapons and things you dont want people to spawn a million of easily the avoid console commands since that will make it easy for the average smuck to take advantage of it with ease, where net messages require a bit more skill and effort to take advantage of.

1 Like

I used both net messages and console commands to run functions in my Custom Health addon, this way people can either heal themselves by opening the menu or by using a bind.

1 Like

true, like there were alot of things that were in console commands that anyone could type in, but i think it checked if you are admin (coded into the gamemode admin i think) on the server before doing what it should, if you were not nothing happened, stuff like that is useful for console commands, that way important things can be binded and done quickly.

1 Like