Have a menu button send sound to everyone? Which direction to take?

Trying to make a menu a button that plays a sound… I want to it be heard by all players even though I technically will be on the server too.

Do I still use surface command? What hook should I use, should I create a variable and then make an if statement on the client_side that leads to a playsound command?

I know net code is used for something like this… But I’m not sure how to begin to structure it. The wiki helped some but I could use a nudge in the right direction.

Simplest way would be to use surface.PlaySound with a BroadcastLua, but if you want to be more secure you should use net library or a user message.

I did this for my gmcc gamemode. Here:

The bit of code you’re looking for is in net.lua and cl_net.lua.

In this example, when the player dies I send a netmessage to play a sound for just them.
The sound in question is randomly picked from a table of sounds.

function PlayDiedSound( ply )

	if !IsValid( ply ) || !ply:IsPlayer() then return end
	net.Start( "PlayerDied" )
		net.WriteString( table.Random( GotKillSounds ) )
	net.Send( ply )


Clientside I receive the message and play the sound.

net.Receive( "PlayerDied", function()

	surface.PlaySound(  net.ReadString()  )

end )

You could probably just make 1 SendSound netmessage and use that for every surface sound you want to play.

The reason I’m not using EmitSound() here is because I only want the one player to hear it.
If you want it to be heard by everyone you could either broadcast it (instead of sending it to one person) or use EmitSound().

Don’t know if this is a good way of doing it, but it’s how I did it and it works like a charm.

Edit: to broadcast it, do this on the server:

function PlayDiedSound()
	net.Start( "PlayerDied" )
		net.WriteString( table.Random( GotKillSounds ) )


I’m not worried about security, this will be only for a local server.

The activation isn’t a player death but the press of a button in a derma menu. The tricky part is I’m not sure which that that should appear in (still wrestling with understanding of client vs server). The client will be pressing the button so that should somehow go to the server to be sent back to clients?

Send a net message to the server when a player presses the derma button. Upon the reception of the net message serverside, either play the sound using

sound.Play if you want to play it serverside or network it and use

surface.PlaySound if you wish to do it clientside. Here’s a tutorial on net library usage if you are not familiar with it.