Getting a sound to play from a local script but get the server to hear the sound?

How would one play a sound from a local script, but get the server to also hear the sound?

I know i could just set RespectFilteringEnabled to false, but i dont wan’t this because i need some sounds to be local. And having RespectFilteringEnabled true will also prevent exploiters from playing a sound locally and have it play for the entire server.

Help would be much appreciated!

4 Likes

Utilize RemoteEvents and pass through the sound you want played.

-- LocalScript
workspace.RemoteEvent:FireServer(SoundObject)

-- Server Script
workspace.RemoteEvent.OnServerEvent:Connect(function(Player, Sound)
    Sound:Play()
end)

Of course, you wouldn’t be storing your events in workspace, instead it’d be something like ReplicatedStorage. And as I’ve said before, you’ll need to specify the sound object you’re playing in the FireServer argument.

Edit: If you want to go the extra mile, you can also fire the sound object to all clients except the player that fired the event by using RemoteEvent:FireClient(player).

12 Likes

I can’t do this via client side can I?

No, FireClient has to be fired from the server.

What should i do instead then?

In the server script, run a for loop that cycles through the list of players, like this:

for _, player in pairs(game:GetService('Players'):GetPlayers()) do
    RemoteEvent:FireClient(player, Sound)
end

However, if you want to use this method, you’ll need a local script on the client that listens for the OnClientEvent. It’d work like this:

workspace.RemoteEvent.OnClientEvent:Connect(function(Sound)
    Sound:Play()
end)

Though if this isn’t the method you’re looking for, just do the first method I posted above where you only fire the sound on the server. The problem with this is that the time at which the sound would be played between when the event was fired will be noticeable, since you’re passing information from the client to the server.

2 Likes

Although I do not recommend this you can use SoundService.RespectFilteringEnabled to be able to play sounds from the client which are replicated to the server. (Exploiters could use to play nasty sounds in your game) I recommend using what @DesiredFlamingFire has stated above.


On another note, you could use RemoteEvent.FireAllClients instead of individually firing the remote for each player.

1 Like

What you’re saying is that you shouldn’t disable SoundService.RespectFilteringEnabled, since exploiters then can play nasty sounds in-game which everyone can hear. Isn’t it exactly the same since you’re just asking the server to play this song for everyone?

As OP has stated, they do not want to disable RespectFilteringEnabled as exploiters can use this, and they want some sounds to stay local. (For example: gun shots, ui clicks etc.) @Ethanthegrand14 if you want to accomplish this, you will have to fire a RemoteEvent from the client.

But, if you also want to prevent bad songs from being played, you should probably add a whitelist system to see that the song is allowed to be played on the server.

For example:

-- // This code is for demonstration purposes only
----

-- List of allowed sounds
local AllowedSounds = {1234, 5678, 9012, 3456};

-- Event to replicate the music
.OnServerEvent:Connect(function (player, music)
    -- If the music is in the AllowedSounds list, then...
    if music isIn AllowedSounds then
        -- Let all the clients hear it
        remote:FireAllClients("play", music)
    end
end)

Also only fire the sounds you want the other clients to hear :stuck_out_tongue:

1 Like

The server doesn’t have an audio device, it can’t hear sounds. I think what you meant to say is that you want sound playback to replicate from a LocalScript? Only two options here are remotes and RespectFilteringEnabled off. The latter is not at all recommended. Honestly I hope it someday gets forced just like FilteringEnabled was.

2 Likes

I’ve managed to get this to function and it works as intended, Thanks!

I guess the ability to play and replicate sounds from a local script is for the smooth first person experience. If remotes are used instead, there will be a lag, right? The player will trigger a sound, but will wait for the remote to fire server and then the sound playing on the server to be replicated back to him. Is there any other way to avoid it, if we want to have RespectFilteringEnabled on?

There is a way to avoid the sound replicating back to the player when firing the information to the server. As mentioned in my first post, you can send the sound information to each client by using this in the RemoteEvent function:

for i, v in pairs(game:GetService("PlayerService") do
    if v ~= Player then
        RemoteEvent:FireClient(v, Sound)
    end
end

Then you’d need to set up a local script which receives the OnClientEvent event from the remote to play the sound.

1 Like

Er… your code sample is malformed. Did you mean to write this?:

for _, vPlayer in ipairs(game:GetService("Players"):GetPlayers()) do
    if vPlayer ~= Player then
        RemoteEvent:FireClient(vPlayer, Sound)
    end
end

Whoops, didn’t see that. Thanks for catching it.