Dropping alternate weapons - unsure how to code.

Hey guys,

I’m trying to make a weapon on TTT that when you drop it, it turns into a different weapon. The dropped weapon needs to have the same ammo as the dropped weapon.

Here’s what I’ve got so far:
[lua]function SWEP:onDrop()

And that’s all I really have right now.
Suppose the weapon I want to drop is “weapon_ttt_m4”, could someone show me how to do it?

Thanks in advance!

**EDIT #1: **
Current code is:
[lua]function SWEP:OnDrop()

Current issue: No entity is created on drop.


Thanks for the fast reply!

Using that, how would I set the ammo for that gun?

[editline]22nd April 2016[/editline]

I’m assuming it has something to do with SWEP:Ammo1 and SWEP:Ammo2.

Anyone have an idea?


Nevermind about that. What I need to figure out now is how to actually spawn the weapon correctly. I’m assuming it isn’t working as it needs a position to go to…?

Anyone have any ideas?

try make a blank entity with the same model as the wepaon that just gives people the gun and desired ammo when they press e on it

First, ents.create at the weapons position, then simply remove the old weapon, should work?

Sorry, my lua coding skills aren’t so good.

By “ents.Create at the weapons position”, do you mean after the drop? In TTT the guns go forward a bit, so yeah.

Thanks in advance.


When trying to create a weapon using ents.Create, no weapon appears.

Current code is:
[lua]function SWEP:OnDrop()

The weapon just gets removed, and no weapon spawns.
I tried using ply:Give and ply:DropWeapon, but the same happens.

You never even spawned the entity… try looking at the example on the

ents.Create page

[editline]23rd April 2016[/editline]

Also, you could set the weapon’s ammo with

Weapon:SetClip1 and

Weapon:SetClip2 (I think)

after the entity is created you should probably set the positions, angles, apply some force to it, and then call Spawn() on it so it starts networking to the client. currently the entity is being created, its just spawning at the map’s origin point

Also, before you ask about setting the weapon’s position and angles (since the weapon would probably spawn at Vector(0,0,0) by default, which you obviously don’t want), just use



Entity:SetPos and


[editline]23rd April 2016[/editline]


[editline]23rd April 2016[/editline]

Well, if you get stuck with that, [sp]this might be helpful to you: pastebin.com/8b790ZTD[/sp]

Just wanna give a quick hand:
You can store the first weapons position and angles in variables right before it gets deleted. Then you can use those variables when setting the new weapons position and angles



Alright, thanks everyone. I’ll try not to look at that pastebin so I can learn it for myself.

[editline]24th April 2016[/editline]

I tried, however still no weapon spawns.
I looked at the pastebin and I couldn’t find any differences.
Here’s my current code:
function SWEP:OnDrop()
local ent = ents.Create(“weapon_ttt_m16”)
ent:SetPos( self:GetPos() )
ent:SetAngles( self:GetAngles() )
ent:SetClip1( self:Clip1() )
ent:SetClip2( self:Clip2() )
local phys = ent:GetPhysicsObject()
if IsValid(phys) then
phys:SetVelocityInstantaneous(Vector(0,0,-75) + phys:GetVelocity() * 0.001)
phys:AddAngleVelocity(phys:GetAngleVelocity() * -0.99)
I put the Velocity stuff in to make it drop like a normal weapon. Removing all the velocity stuff still spawns nothing.

[editline]24th April 2016[/editline]

When I try to check if then ent is valid (by doing if !IsValid( ent ) ), it doesn’t print anything to console, even though it should be. It’s like the onDrop function doesn’t run.
I’m an idiot. I didn’t check the server console. Getting an error, will try to fix it.[/SUB]
Found the cause of my issues. Screwed up my file structure. Code works now, however it spawns on me, and instantly gives me the weapon back. Obviously just need to change the positioning a bit.