How to play a sound when weapon is selected? & how to delete a prop after it's been thrown?

Hello! I’m making a Garry’s Mod SWEP and I’m pretty much done with it except I would like to know how to make the weapon play a sound when it’s selected. Also, my SWEP on left click will fire a fist and play a sound, the problem is I don’t know how to despawn the prop after it’s been fired. I bet you can see why this could be annoying, lag.

Here is my code:

SWEP.PrintName = “Gomu Gomu No Pistal” – This will be shown in the spawn menu, and in the weapon selection menu
SWEP.Author = “MLGSniperKing” – These two options will be shown when you have the weapon highlighted in the weapon selection menu
SWEP.Instructions = “Gomu Gomu No!!!”

SWEP.Spawnable = true
SWEP.AdminOnly = true

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = “none”

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = “none”
SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true

SWEP.ViewModel = “models/weapons/v_smg1.mdl”
SWEP.WorldModel = “models/weapons/w_smg1.mdl”

local ShootSound = Sound( “sound/weapons/one piece/fist/pistal” )

function SWEP:PrimaryAttack()

self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )

-- Call 'ThrowChair' on self with this model
self:ThrowChair( "models/props/luffy/fist/fist.mdl" )

end


– Called when the rightmouse button is pressed

function SWEP:SecondaryAttack()

-- Note we don't call SetNextSecondaryFire here because it's not
-- automatic and so we let them fire as fast as they can click.


self:ThrowChair( "models/props/luffy/fist/fist.mdl" )

end


function SWEP:ThrowChair( model_file )

--

--
self:EmitSound( ShootSound )


--

if ( CLIENT ) then return end

--
-- Create a prop_physics entity
--
local ent = ents.Create( "prop_physics" )


if ( !IsValid( ent ) ) then return end

--

ent:SetModel( model_file )

--
--
--
ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
ent:SetAngles( self.Owner:EyeAngles() )
ent:Spawn()


--

local phys = ent:GetPhysicsObject()
if ( !IsValid( phys ) ) then ent:Remove() return end



local velocity = self.Owner:GetAimVector()
velocity = velocity * 10000000
velocity = velocity + ( VectorRand() * 100 ) -- a random element
phys:ApplyForceCenter( velocity )


cleanup.Add( self.Owner, "props", ent )

undo.Create( "Thrown_punch" )
	undo.AddEntity( ent )
	undo.SetPlayer( self.Owner )
undo.Finish()

end

Thanks!

Hi,

Try and redo this line

local ent = ents.create

It looks right but just double check I don’t really make SWEPS I do cars and such but it looks no diffrent

-----Edit------

It looks right? Maybe you didn’t define it right just my guess

Well my SWEP works fine. What I need to know is how to make a sound when you select the weapon and how to delete the props after x amount of time. Also my SWEP won’t play the sound when clicked, I checked the directory to the wav file but I won’t play :confused:

Do this somewhere:

hook.Add(“PlayerSwitchWeapon”, “UNIQUENAMEHERE”, function(ply, ow, nw)
local plyPos = ply:GetPos()
if nw == “YOUR WEAPON NAME GOES HERE” then
sound.Play(“SOUND NAME HERE”, plyPos, 50, 100, 1)
end
end

Thats just my guess, anyway…

Edited:


SWEP.PrintName	= "Gomu Gomu No Pistal" -- This will be shown in the spawn menu, and in the weapon selection menu
SWEP.Author	= "MLGSniperKing" -- These two options will be shown when you have the weapon highlighted in the weapon selection menu
SWEP.Instructions	= "Gomu Gomu No!!!"

SWEP.Spawnable = true
SWEP.AdminOnly = true

SWEP.Primary.ClipSize	= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic	= false
SWEP.Primary.Ammo	= "none"

SWEP.Secondary.ClipSize	= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo	= "none"
SWEP.Weight	= 5
SWEP.AutoSwitchTo	= false
SWEP.AutoSwitchFrom	= false

SWEP.Slot	= 1
SWEP.SlotPos	= 2
SWEP.DrawAmmo	= false
SWEP.DrawCrosshair	= true

SWEP.ViewModel	= "models/weapons/v_smg1.mdl"
SWEP.WorldModel	= "models/weapons/w_smg1.mdl"

local ShootSound = Sound( "sound/weapons/one piece/fist/pistal" )

function SWEP:PrimaryAttack()


self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )

-- Call 'ThrowChair' on self with this model
self:ThrowChair( "models/props/luffy/fist/fist.mdl" )

end

--
-- Called when the rightmouse button is pressed
--
function SWEP:SecondaryAttack()

-- Note we don't call SetNextSecondaryFire here because it's not
-- automatic and so we let them fire as fast as they can click.


self:ThrowChair( "models/props/luffy/fist/fist.mdl" )

end

--

--
function SWEP:ThrowChair( model_file )

--

--
self:EmitSound( ShootSound )


--

if ( CLIENT ) then return end

--
-- Create a prop_physics entity
--
local ent = ents.Create( "prop_physics" )


if ( !IsValid( ent ) ) then return end

--

ent:SetModel( model_file )

--
--
--
ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
ent:SetAngles( self.Owner:EyeAngles() )
ent:Spawn()


--

local phys = ent:GetPhysicsObject()
if ( !IsValid( phys ) ) then ent:Remove() return end



local velocity = self.Owner:GetAimVector()
velocity = velocity * 10000000
velocity = velocity + ( VectorRand() * 100 ) -- a random element
phys:ApplyForceCenter( velocity )

timer.Simple(3, function() ent:Remove() end)
cleanup.Add( self.Owner, "props", ent )

undo.Create( "Thrown_punch" )
undo.AddEntity( ent )
undo.SetPlayer( self.Owner )
undo.Finish()
end

Should remove the prop after 3 seconds.

Try using the

SWEP:Deploy function… but it seems you may need to do a little networking with that function