Weapon Sound

I am working on a pistol swep for gmod. But how do I make it so it will use a custom sound?

Add this line to SWEP:Initialize()


local pewpew = Sound("sounds/path/to/sound")

then in SWEP:PrimaryAttack()


self.Weapon:EmitSound(pewpew)

Sorry for the bump, but would this work for a reload sound also?

Of course, your SWEP won’t ever emit sound until you tell it too.

Ok, then what would the code be?

[lua]local sounds = {}
sounds[1] = {path = “sounds/path/to/sound”}
sounds[2] = {path = “sounds/path/to/sound”}

function SWEP:PrimaryAttack()
for k,v in pairs( sounds[1] ) do
self.Weapon:EmitSound(v.path)
end
end

function SWEP:Reload()
for k,v in pairs( sounds[2] ) do
self.Weapon:EmitSound(v.path)
end
end[/lua]

Untested, but should work.

Sorry Helioth but that’s kind of an inefficient method.

Here’s something a bit simpler :
[lua]self.Primary.Sound = “sounds/path/to/sound1”
self.Reload.Sound = “sounds/path/to/sound2”

function SWEP:PrimaryAttack()
self.Weapon:EmitSound(self.Primary.Sound)
end

function SWEP:Reload()
self.Weapon:EmitSound(self.Reload.Sound)
end[/lua]

To make it so you cant play it multiple times at the same time, use…

[lua]SWEP:Primary.Sound = “sounds/path/to/sound1”
SWEP:Reload.Sound = “sounds/path/to/sound2”
SWEP.NextAttack = 0
SWEP.AttackTime = 5 --Time of Primary Fire Sound here
SWEP.NextReload = 0
SWEP.ReloadTime = 5 – Time of Reload Fire Sound here

function SWEP:PrimaryAttack()
if self.NextAttack> CurTime() then return end
self.Attack= CurTime() + self.AttackTime
self.Weapon:EmitSound(self.Primary.Sound)

end

function SWEP:Reload()
if self.NextReload > CurTime() then return end
self.NextReload = CurTime() + self.ReloadTime
self.Weapon:EmitSound(self.Reload.Sound)
end[/lua]