Play sound on kill with particular SWEP

Hello all,

I’ve got a struggle again. I want when someone kills a NPC/ player with my custom gun, it plays a ‘death sound’.
So for example: I kill Dr. Kleiner, and when he’s dead, it plays a song (for example). But I think it’s a really small line of code, but couldn’t find it anywhere else.

Thanks in advance.

Take a look at these:


Can’t figure it out, I don’t understand the GM/PlayerDeath. :confused:
So how to like play a sound (I know how to do that) when a player killed another player/npc with my particular SWEP?

The hook PlayerDeath is “called whenever a player dies”. It gives you the player that is dead, the inflictor entity (in your case, you swep) and the killer. You just have to check whether the inflictor is your swep or not and you’re done.

Ok, this is what I have, but which swep name (Just the name, or a the files name…? etc.) do I have to fill in?

function GM:PlayerDeath( ply, inflictor, attacker )
if ( inflictor:WhereToFindMySWEPName() then
surface.PlaySound( “weapons/kill.wav” )

[lua]inflictor:GetClass( ) == “weapon_css_colt_m4a1”[/lua]

is probably the boolean logic you’re looking for.

So like this? But where to find the swep name? (I just called it my_swep for now)
Sorry, I just started learning lua. :3

inflictor:GetClass( ) == “my_swep”
function GM:PlayerDeath( ply, inflictor, attacker )
if ( inflictor:GetClass() then
surface.PlaySound( “weapons/kill.wav” )


Like this:

function GM:PlayerDeath( ply, inflictor, attacker )
     if inflictor:GetClass() =="my_swep" then
          surface.PlaySound( "weapons/kill.wav" )

That’s not going to work since “surface.PlaySound()” is clientside.

Do you have a suggestion which is better? :slight_smile:

To find the name of your swep, it’s either the single lua file in entities/weapons/*.lua or a folder name in the same directory.

I used weapon_css_colt_m4a1 because I have a lua file, which is shared, called weapon_css_colt_m4a1.lua in that directory.

You could use BroadcastLua( ) if you want all clients to hear it, or look into net.Start( msgname ), net.WriteString( ), net.Send( client ) or net.Broadcast( ), net.Receive( ), util.AddNetworkString( msgname ) to network the sound name and send it to the client.

In this case, BroadcastLua might be your best bet.

surface.PlaySound will play a sound without any 3d effect. That’s to say that if you want a player to “scream” upon death, this wouldn’t work well as the sound wouldn’t appear to come from the player, it would be a global sound.

I would use sound.Play and then use


as your Vector Pos.

-snip- nevermind.

You could use Entity.EmitSound.