Entity Take Damage Sound

Hi there again,

When my entity gets damaged I want it to play a “hurt” sound. I came up with the following code but it’s not outputting the sound:



function ENT:OnTakeDamage( dInfo )
	self:EmitSound( "vo/npc/male01/pain0"..math.random(1,9)..".wav", 100, 100 )
end


I have no clue why it won’t work

Thanks,
Computer600

Does any red text appear in the console while you are trying to play the sound?
Also try it with another sound, without math.random().

Try “ambient/levels/labs/electric_explosion5.wav”

No red text/missing sounds for my one and no red text/missing sounds for your one either and “ambient/levels/labs/electric_explosion5.wav” doesn’t play either

[editline]21st February 2014[/editline]

My entity is a NextBot if that helps



function ENT:OnTakeDamage()
self:EmitSound("vo/npc/male01/pain0.wav", 500, math.random(100,200))
end


This should work just fine.

Im not sure about this but since you attempting to add a num to a string?

[lua]
function ENT:OnTakeDamage( dInfo )
self:EmitSound( “vo/npc/male01/pain0”… tostring( math.random( 1, 9 ) ) …".wav", 100, 100 )
end
[/lua]

[editline]22nd February 2014[/editline]

Okay, I used this on my level printer once :slight_smile: This is tested and works!

[lua]
function ENT:OnTakeDamage( dInfo )

self.Sound = CreateSound(self.Entity, "vo/npc/male01/pain0".. tostring( math.random( 1, 9 ) ) ..".wav")
self.Sound:Play()

end
[/lua]