I need to use surface.PlaySound on a gamemode function

I’m trying to get a sound to play on GM:RoundEnd but I cant use surface.PlaySound because it’s clientside, so what do I do?

I’ve tried this but it doesn’t work:
[lua]function GM:RoundEnd()
if (CLIENT) then
surface.PlaySound(“AGU/radio/radio_chatter.mp3”)
end
end[/lua]

any suggestions?

You can either use usermessages or console commands.

Basically, the idea is to send a signal to the client which tells it to start the sound. I’d go for usermessages, so here’s an example:

Serverside:



function GM:RoundEnd()
    umsg.Start( "RoundEndSound" ) --Say "we're sending RoundEndSound"
    --We don't need to send anything here, so don't bother
    umsg.End() --Finish (actually sends the message)
end


Clientside:



usermessage.Hook( "RoundEndSound", function( data )
    --We don't have any data to read, but if we did:
    --local str = data:ReadString()
    --Reading data has to be done in order of how it was sent
    
    --Well we've received our usermessage, this means the server is telling us to play that sound
    surface.PlaySound( "AGU/radio/radio_chatter.mp3" ) --Play the sound
end )


Read the comments in the code, and check out the wiki page here:
**[User Messages

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=User_Messages)**

thanks, but how would I make it play only to specific players? for example, only team 2. is this even possible?

[lua]local rp = RecipientFilter()
for k, v in pairs(team.GetPlayers()) do
rp:AddPlayer(v)
end
umsg.Start(“YourUserMessage”, rp )[/lua]

or

[lua]
for k, v in pairs( player.GetAll() ) do
if( v:Team() == 2 ) then
umsg.Start( “hello”, v )
umsg.End();
end
end
[/lua]

or
[lua]
function GM:RoundEnd()
local sound = “AGU/radio/radio_chatter.mp3”;
for k,v in pairs(team.GetPlayers(PUT_TEAM_NUMBER_HERE)) do
v:SendLua(“surface.PlaySound(%q)”, sound)
end
end
[/lua]

Just an FYI, if you don’t give a second argument to umsg.Start then it sends it to everyone. You can also give it a player object to send to that player alone. Anything else needs a recipient filter.

IIRC, it also accepts tables of players.