Playing a sound only on the local player

Is there a way to play a sound serverside that only the player playing can hear the sound and the players around him wont hear it.

Concommand(“play SOUNDHERE.SOUNDFORMAT”)? :3:

Oh that was easy, thank you.

LocalPlayer():EmitSound is actually the correct way. The concommand is unreliable and doesn’t allow more sound than one.

But LocalPlayer() doesn’t work server side.

Oh, then you can use usermessages or just directly do ply:EmitSound, but everyone will be able to hear it.

[lua]local thesound = Sound(“common/stuck1.wav”)
ply:SendLua( “surface.PlaySound( “” … thesound … “” )” )[/lua]
Use SendLua

how? and whats so bad about concommand?

I gave you an example, it makes the player ‘ply’ run surface.PlaySound()

Don’t use SendLua. You’re unnecessarily sending a string AND I believe surface.PlaySound only allows one sound to be playing at a time.

On the server:
[lua]umsg.Start(“Playsound_Give-It-A-Name”, ply)
On the client:
[lua]usermessage.Hook(“Playsound_Give-It-A-Name”, function ()
LocalPlayer():EmitSound(“sound_filename”, 100, 100)

“Playsound_Give-It-A-Name” can be replaced with any name, it doesn’t matter, so long as it’s the same on the client and server portions of the code I gave you.

If there are multiple sounds you want to play using a similar method, you can also incorporate some tables:
[lua]umsg.Start(“Playsound_Give-It-A-Name”, ply)
umsg.Char(sound-index) --The index of the sound you want, as referenced in the sound table clientside.

local soundtable = {“sound_filename1”, “sound_filename2”, “sound_filename3”} --Table of sounds, in numerical order

usermessage.Hook(“Playsound_Give-It-A-Name”, function (data)
LocalPlayer():EmitSound(soundtable[data:ReadChar()], 100, 100)

I prefer using **[Surface.PlaySound](** to play sounds on the client. You can use usermessages, or you can use SendLua (only if you’re absolutely sure there are no string injections).

Entoros that doesn’t really help as he still needs the server to tell the client to play the sound, the client doesn’t know when, so either usermessages or SendLua would be necessary, usermessages being the most efficient. Whether he wants to use surface.PlaySound or LocalPlayer():EmitSound is up to him, the only real difference is apparently you can only have one sound playing with surface.PlaySound at a time.

Please read the whole post.

Here’s an example of sending the sound to all clients:

if !pl:IsAdmin() then return end

[lua]usermessage.Hook(“playsound_all”,function(um) surface.PlaySound(um:ReadString()) end)[/lua]

Er, huh? The OP’s looking for a way to have the server tell a single client to play a sound that nobody else can hear, not for an admin to tell all clients to hear a sound…

I’m providing an example mate, it’s a part of the learning process.

If the OP can’t take out “RecipientFilter():AddAllPlayers” then the OP needs to learn some more Lua.

Alright, I see your point. I prefer to give specific examples but I suppose a practical one helps learning too.

Ahh thanks guys I will try these out also. I don’t think knowing how to use RecipientFilter has anything to do with learning or knowing lua, just because you know lua doesn’t mean you know what all the garry’s mod functions do and all the ways you can use them.

@CowThing sorry I couldn’t see it cause I cant see anything that are in lua tags.

Rather than having to mess around with “UserMessages” you can use ConCommand server side, As GPBS said, No it isnt “Unreliable” since it works 200% “Yes i said 200%” for me. And LocalPlayer():ConCommand("") would work aswell for client side.

Well, it depends. When sending the concommand “play longfilename.mp3”, you’d be sending (length of the concommand*2) bytes worth of data, or you could pool a usermessage and send around 6 bytes of data (The pooled number can be more or less depending)