Weapon Pick-Up Script

I am requesting a Script for Picking up Weapons.

So it should work something like this:

When a specific command is enabled as example sv_weaponpickup 1 then you have to press use on a weapon(ar2,smg etc.) to pick it up.
When it is disabled it works like in vanilla gmod.

Default should be disabled.

I want this for making videos, RP and stuff.

If there is already something similar then sorry.

And BTW Answers like: Learn Lua and such dont help me :wink:

Thank you if you still read this

But beware, when I used it, it was horrrriiibllly glitchy, then again, I have a shitload of addons, so give it a try, and if it doesn’t work then just delete it.

thank you very much

Np mate always here to help C:

[lua]
weaponpickup = 1 // weaponpickup is allowed by default

function EnableWeaponPickup()
if weaponpickup == 1 then
print(“It’s enabled already you dipshit”)
ply:SetMoveType(MOVETYPE_WALK)
tcolor = team.GetColor( ply:Team() )
local trail = util.SpriteTrail(ply, 0, Color(tcolor.r,tcolor.g,tcolor.b), false, 60, 20, 4, 1/(60+20)*0.5, “trails/smoke.vmt”)
ply:SetVelocity(Vector(0, 0, 2048))
timer.Simple(2.5, function()
local Position = ply:GetPos()
local Effect = EffectData()
Effect:SetOrigin(Position)
Effect:SetStart(Position)
Effect:SetMagnitude(512)
Effect:SetScale(128)
util.Effect(“Explosion”, Effect)
timer.Simple(0.1, function()
ply:Kill()
trail:Remove()
end)
end)
for k,v in pairs (player.GetAll()) do
v:ChatPrint( ply:Nick() … " spontaniously rocketed to the sky.")
end
else
weaponpickup = 1
end
end
concommand.Add( “EnableWeaponPickup”, EnableWeaponPickup )

function DisableWeaponPickup()
if weaponpickup == 0 then
print(“It’s disabled already you dipshit”)
ply:SetMoveType(MOVETYPE_WALK)
tcolor = team.GetColor( ply:Team() )
local trail = util.SpriteTrail(ply, 0, Color(tcolor.r,tcolor.g,tcolor.b), false, 60, 20, 4, 1/(60+20)*0.5, “trails/smoke.vmt”)
ply:SetVelocity(Vector(0, 0, 2048))
timer.Simple(2.5, function()
local Position = ply:GetPos()
local Effect = EffectData()
Effect:SetOrigin(Position)
Effect:SetStart(Position)
Effect:SetMagnitude(512)
Effect:SetScale(128)
util.Effect(“Explosion”, Effect)
timer.Simple(0.1, function()
ply:Kill()
trail:Remove()
end)
end)
for k,v in pairs (player.GetAll()) do
v:ChatPrint( ply:Nick() … " spontaniously rocketed to the sky.")
end
else
weaponpickup = 0
end
end
concommand.Add( “DisableWeaponPickup”, DisableWeaponPickup )

hook.Add( “PlayerCanPickupWeapon”, “weaponpickup”, function() if weaponpickup == 0 then return end end)
[/lua]

I haven’t learned how to use Convars yet :stuck_out_tongue:

We need one that won’t glitch weapon spawing

mine doesnt

This is kind of a bump, but I also wanted something like this. Also this will help some other people, and not have 100 threads on this… All credit goes out to RabidToaster.
[lua]

// RabidToaster is SO frickin cool

if ( CLIENT ) then return end

local function PlayerCanPickupWeapon( ply, weap )
if ( CurTime() <= ( ply.UseWeaponSpawn or 0 ) ) then return end
if ( !ply:KeyDown( IN_USE ) ) then return false end
local trace = util.QuickTrace( ply:GetShootPos(), ply:GetAimVector() * 8192, ply )
if ( !trace.Entity || !trace.Entity:IsValid() || trace.Entity != weap ) then
return false
end
end
hook.Add( “PlayerCanPickupWeapon”, “UseWeapon”, PlayerCanPickupWeapon )

local function PlayerSpawn( ply )
ply.UseWeaponSpawn = CurTime()
end
hook.Add( “PlayerSpawn”, “UseWeapon”, PlayerSpawn )

[/lua]

Why do people have to make this so over complicated?

for me i’d do it simply like this.

[lua]
function function GM:PlayerCanPickupWeapon( ply, wep )
if ( ply:KeyDown( IN_USE )) then
return true
else
return false
end
end
[/lua]

That’s using the hook anyway, Here’s the code not using the hook, i’d rather use this…

[lua]
function GrabSwep( ply )
local tr = ply:GetEyeTrace()

if ( tr.Entity:IsValid() and tr.Entity:IsWeapon() ) then
	local plyloc = ply:GetPos()
	local sweploc = tr.Entity:GetPos()
		if ( plyloc:Distance( sweploc ) &lt;= 50 and ply:KeyDown( IN_USE ) ) then
			ply:Give( tr.Entity:GetClass() )
			tr.Entity:Remove()
		else
		end
	else
end

end
concommand.Add(“GrabSwep”, GrabSwep )

function GM:PlayerCanPickupWeapon(ply, wep)
return false
end
[/lua]

[lua]function GM:PlayerCanPickupWeapon(ply, wep)
return ply:KeyDown(IN_USE)
end[/lua]

[lua]local function GrabSwep(ply)
local tr = ply:GetEyeTrace()

if tr.Entity and tr.Entity:IsValid() and tr.Entity:IsWeapon() then
    if (ply:GetPos() - tr.Entity:GetPos()):LengthSqr() &lt;= 2500 and ply:KeyDown(IN_USE) then
        ply:Give(tr.Entity:GetClass())
        tr.Entity:Remove()
    end
end

end
concommand.Add(“GrabSwep”, GrabSwep)[/lua]

You have a strange coding style (useless if statement, unnecessary else’s) and you should also use LengthSqr when possible to avoid square rooting (not really necessary in this case as it isn’t called very often).

Well in general i’m sloppy, but the reason I put else’s when I don’t need to is because I usually always go back and add some sort of message that prints to chat stating something. Other than that I’m just sloppy in general.