Stop sound is not working.

Basically, when you die, the weapon will stop its sound. But it is not working for some reason. It’ll just keep the playing.

– This code starts the song on equip


function SWEP:Deploy()
timer.Simple(0.1, function()
self.Owner:SetModel("models/player/combine_super_soldier.mdl")
self.Owner:SetHealth( 300 )
self.Weapon:EmitSound( self.TauntSound )

end)
end

– this defines the sound


SWEP.TauntSound = Sound( "jugnat.wav" )

– This code stops* the song on equip, or is suppose to :confused:


function SWEP:OnDrop()
self.Weapon:StopSound( self.TauntSound )
self:Remove()
end

StopSound doesn’t do anything/broken.
Also, don’t use self.Weapon, use just self.

Alright I changed the self.Weapon to just self

But if StopSound is broken is there any other solutions to stopping the sound?

Use the Play and Stop functions for CSoundPatch, so you couldn’t use EmitSound if you want to stop it.

Unless you want to stop ALL sounds by executing stopsound command on the player.

It gave me this error

[ERROR] gamemodes/terrortown/entities/weapons/weapon_ttt_juggernaut/shared.lua:14: attempt to index global ‘CSoundPatch’ (a nil value)

  1. unknown - gamemodes/terrortown/entities/weapons/weapon_ttt_juggernaut/shared.lua:14

Timer Failed! [Simple][@gamemodes/terrortown/entities/weapons/weapon_ttt_juggernaut/shared.lua (line 11)]

This is what I used


function SWEP:Deploy()
timer.Simple(0.1, function()
self.Owner:SetModel("models/player/combine_super_soldier.mdl")
self.Owner:SetHealth( 300 )
CSoundPatch:Play( "jugnaut.wav" )

end)
end

That’s not how it works. I added an example to the page linked above.

Alright I fixed this, with this code.


function SWEP:OnDrop()
RunConsoleCommand("stopsound");
self:Remove()
end

That is by all means the dumbest way to go about it.

Then what would you suggest?

I’m all out of ideas.

EDIT: didint see your second post, im testing it.

This is bound to help you



function SWEP:Deploy()
timer.Simple(0.1, function()
self.Owner:SetModel("models/player/combine_super_soldier.mdl")
self.Owner:SetHealth( 300 )
local mysound = CreateSound( self,"self.TauntSound" )
mysound:Play()

end)
end


The above could use some tuning… But should work
Although i think its better to stuff “local mysound = CreateSound( self,“self.TauntSound” )” into SWEP:Initialize() instead



function SWEP:OnDrop()
mysound:Stop()
self:Remove()
end


Keep in mind you may want to define mysound outside the hooks.

I changed the previous code I had to this.


function SWEP:OnDrop()
for k,v in pairs(player.GetAll()) do
    v:ConCommand("stopsound")
end
self:Remove()
end

EDIT: Oh my, more suggestions :slight_smile:

Alright It’s fixed, thank you for all the suggestions and help!

Cheers.

Replace “local mysound” and “mysound” with “self.mysound” for SWEPs.