Hitsound in gamemode

I want to make a hitsound when player shoots an other player. I think it should be something like


function GM:EntityTakeDamage( )
 
	if ( ent:IsPlayer() ) then
		self.Owner:EmitSound(hitsound)
	end
 
end

But sure it’s wrong and I don’t know how to write it right.

Try this.


function GM:EntityTakeDamage( )
 
    if ( ent:IsPlayer() ) and dmginfo:IsBulletDamage() then
        self.Owner:EmitSound("Path/To/Sound.whatever")
    end
 
end


[ERROR] gamemodes/q3ffa/gamemode/init.lua:27: attempt to index field 'Owner' (a nil value)


function GM:EntityTakeDamage( )
 
    if ( ent:IsPlayer() ) and dmginfo:IsBulletDamage() then
         ent:EmitSound("Path/To/Sound.whatever")
    end
 
end

You forgot to add the args to the first line


function GM:EntityTakeDamage( ent, dmginfo )

Now the sound is coming from the player gets damage, and I need to play the sound on the player shoots

What kind of hitsound is it? If you want a Quake-like hitsound that notifies a player every time they hit someone, only the attacker should be hearing it. Try this instead:

Serverside:
[lua]function GM:EntityTakeDamage(ent, dmginfo)
local attacker = dmginfo:GetAttacker()

if ent:IsPlayer() and attacker:IsPlayer() then
	umsg.Start("PlayHitSound", attacker)
	umsg.End()
end

end[/lua]

Clientside:
[lua]usermessage.Hook(“PlayHitSound”, function(msg)
LocalPlayer():EmitSound(“path/to/sound.whatever”)
end)[/lua]

This will not play a sound serverside, instead it will send a message to the attacking player which plays a sound clientside once it is received. No one else will be able to hear it.

Thank you!