Weapon pickup on use

I was wondering how I could get the player to pick up a particular weapon when he pressed his +USE key on it. I tried using the PlayerUse, PlayerPickedUpWeapon, and WeaponEquip functions together to no avail in any shape form or fashion. so, How would I go about making you only pick up the weapon when you press +USE on it?

There are many ways to go about doing this. Some will suggest a hook that disables picking up weapons unless you’re looking at them and using E (and a bunch of other conditions). And that works.

Personally I would suggest you make a weapon entity that you can spawn and set the model of as well as giving it the class name of the weapon it represents in a variable. This entity would give the weapon to a player that uses it and then remove itself. This method allows you to fully customize how you want weapons on the ground to act and is my favorite.

note : If you just want a script to dump on your server so that everything works go with the hook (as soon as someone suggests one :v:). If you’re making a gamemode you should go with the entity.

[editline]1[/editline]
Oh and since someone has to mention it, please use the search button next time. :buddy:
This question has been asked and answered more times then I care to count.

http://www.garrysmod.org/img/?t=dll&id=83820
Dunno if its the right way to do it but it works. Made long time ago for another one of these threads…
Also that only works on Dropped SWEPs weapons in its current form.

It’s for a gamemode, so I think I’ll just create my own weapon entity.

I was also thinking of adding a L4D2-style border to the weapons using render / draw, I’m not sure which one would be the best bet just yet since I haven’t thought on how to do this nor have I looked over the functions very much for either one. I was thinking that I could just take the base_gmodentity’s border, and make it constant, and do about 10 different ones, each having 25 more alpha than the last, or something like that. Ah well, I’ll try it out now and see how wel it works out, I’ll repo back with results.

[lua]
hook.Add(“PlayerCanPickupWeapon”, “UsePickup”, function(ply, wep)
if (ply:KeyDown(IN_USE)) then
return true
end

return false

end)
[/lua]

I’ve once written a script to do this:

[lua]hook.Add( “PlayerSpawn”, “PickupTimeout”, function( ply )
ply.PickupTimeout = CurTime() + 0.5
end )

hook.Add( “PlayerCanPickupWeapon”, “NoPickup”, function( ply, wep )
if ( ( ply.PickupTimeout or 0 ) < CurTime() ) then return false end
end )

hook.Add( “KeyPress”, “PressUse”, function( ply, key )
if ( key == IN_USE ) then
local tr = ply:GetEyeTrace()
if ( ValidEntity( tr.Entity ) and tr.Entity:IsWeapon() and tr.Entity:GetPos():Distance( ply:GetShootPos() ) < 64 ) then
ply.PickupTimeout = CurTime() + 0.5

		ply:Give( tr.Entity:GetClass() )
		tr.Entity:Remove()
	end
end

end )[/lua]

Tested and works.

If you use Entity:SetModelScale( -1.1, -1.1, -1.1 ) and then turn the color black and draw it, then reset the model scale and the color and draw it again, then you have a basic outline. Change the color, increase the scale with a for loop, etc and you’ll have a nice outline :buddy:

Well, I tried my own solution, and it didn’t do diddly squat. Apparently, when I used the onentitycreated hook, I checked if it was a weapon, if so, I got it’s model and class. Spawned my phony one, and set the model of the new entity that would replace the swep. I went to set the model on the entity and it would give me an error saying that there was a string expected, got nil, when I simply did “weapon:SetModel(ent:GetModel())”, ah well, I’ll try a few of the ones above and see how they work out.

So just for the record, the return values of hooks DO have an impact on these events? Because I remember in earlier versions of garrysmod, the return value of dynamically added hooks like that were ignored (so you could create a hook that did something on prop spawn but not a hook that would stop the prop spawn - that ability was reserved for the main event in the gamemode)

Well the thing is, instead of checking when a weapon is created directly create your custom entity instead. no need to go through so many steps.

It all depends on the hook. There are actually 2 hooks for prop spawn, one to allow or disallow it and then one to do something with the spawned entity.

Im pretty sure i gave KungFuJew some scripts that did this <.>