Client Sided Sounds

I apologize if this is in the wrong category.

Lately, I’ve been trying to make sounds play for the client only, and not for the server, but I cannot seem to do it.

  1. What do you want to achieve? I want sounds to play only for the client, and not the server.

  2. What is the issue? Whatever I do, it doesn’t work as intended.

  3. What solutions have you tried so far? I searched for similar posts, and looked at the Developer Hub, but I didn’t see my answer.

I also have tried setting SoundService.RespectFilteringEnabled to true, but everytime, the result was either it was playing for the entire server, or no sound played at all. Any help is appreciated.

Edit: The sound is supposed to play when a part is touched.

Double Edit: Here is the script that plays the sound.

local debounce = false

game.Workspace.BadRamp4.Touched:Connect(function(hit) --Change bad part name
    if not debounce then
	    debounce = true
	    print("Oof!")
	    game.Workspace.BadRamp4.Material = Enum.Material.Neon --Change bad part name
	    game.Workspace.Whoops:Play()
	    wait(1)
	    game.Workspace.BadRamp4.Material = Enum.Material.SmoothPlastic --Change bad part name
	    debounce = false
    end
end)
2 Likes

Here is an example on how to do it in localscript:
At this source you should put your audio in ServerStorage.

local sound = game.ServerStorage:FindFirstChild("SoundName") --change to your sound name
sound:Play()

You should use localscript because it’s called on localplayer and at this point we only play audio on localplayer.
I hope this helps. :herb:

4 Likes

The Play() Function is in a local script, but it still doesn’t play for only clients.

What about if you parent the sound into a player and play it from player? but I am pretty sure it should work only for clients if you call play from localscript. :thinking:

script inside part:

script.Parent.Touched:Connect(function(hit)
     if hit.Parent:FindFirstChild("Humanoid") then
        game.ReplicatedStorage.RemoteEvent:FireClient()
     end
end)

Sound and parent of it is this localscript in StarterGui:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
       script.Parent:Play()
end)

Put the sound into starter gui and only the client hears it

2 Likes

That doesn’t work either. I also tried putting it in StarterPlayer as well.

Are you sure? Have you tested it other people? Because anything in starter gui is only client related

Try this - Creating the sound inside CurrentCamera, setting the SoundId and then playing it.


local camera = workspace.CurrentCamera

-- Creating Sound --
local sound = Instance.new("Sound")
sound.Parent = camera

sound.SoundId = "blah"
sound:Play()
1 Like
local services = setmetatable({},{
    __index = function(self,key)
        return game.GetService(game,key) or nil
    end
})
local soundservice = services.SoundService
local id = 49584959 -- insert id here
soundservice:PlayLocalSound(id)

this script should work on the client, if you wanted to make it into a client even you could also convert that

I don’t think LocalScripts can access ServerStorage?

4 Likes

About two years ago I used to keep all my local sounds in StarterGui, however an update in 2017-18 made it so that sounds could not be heard in StarterGui; I am unsure if this was ever fixed.

1 it’s a server side trigger. So you need to fire a server to client event to tell the client to play the sound.

  1. You need the sound to be accessible for the client. So if you put into StarterPack or StarterGui then it will be copied to the client when they start.

Alternatively, you may be able to put it into Replicated Storage so the client can access it.

First, make sure your event is firing. So just have in the client local script,
"print(“Sound is playing here”) and make sure that comes up.

Then get the code to play the sound that’s accessible for the client. It’s just the play sound code then.

Additionally, Debounce it so you aren’t firing it over and over.

Ah. Now I’m convinced I understand what the problem is.

There’s no code provided, but I’m going to go ahead and assume you’re either not handling this interaction from a LocalScript or you aren’t checking if the player who touched the part is the LocalPlayer, which causes the supposed “sound playback replication” issue you’re experiencing.

Related:

1 Like

Sorry, I’ll provide some code. It is indeed in a LocalScript.

local debounce = false

game.Workspace.BadRamp4.Touched:Connect(function(hit) --Change bad part name
    if not debounce then
	    debounce = true
	    print("Oof!")
	    game.Workspace.BadRamp4.Material = Enum.Material.Neon --Change bad part name
	    game.Workspace.Whoops:Play()
	    wait(1)
	    game.Workspace.BadRamp4.Material = Enum.Material.SmoothPlastic --Change bad part name
	    debounce = false
    end
end)

The script works as intended, but the sound just isn’t client sided.

Here is your fixed code:

local debounce = false
local sound = game.Workspace.Whoops:Clone()
sound.Name = "Whoops"
sound.Parent = game.Players.LocalPlayer

game.Workspace.BadRamp4.Touched:Connect(function(hit) --Change bad part name
    if hit.Parent:FindFirstChild("Humanoid") then
        if not debounce then
    	    debounce = true
    	    print("Oof!")
    	    game.Workspace.BadRamp4.Material = Enum.Material.Neon --Change bad part name
    	    game.Players:FindFirstChild(tostring(hit.Parent)):FindFirstChild("Whoops"):Play()
    	    wait(1)
    	    game.Workspace.BadRamp4.Material = Enum.Material.SmoothPlastic --Change bad part name
    	    debounce = false
        end
      end
    end)

I hope this helps. :herb:

Refer to the post I put up. You will need to check if the part that touched your detector is a descendant of the character and run this from a LocalScript.

@ArticGamerTV Checking for the presence of a Humanoid or changing the location of the sound is not sufficient enough. You will still experience global playback if you do not explicitly check who touches the detector.

Easy way of going about it:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

game.Workspace.BadRamp4.Touched:Connect(function(hit) --Change bad part name
    -- Change starts here and ends off next line
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if (player and player == LocalPlayer) and not debounce then
	    debounce = true
	    print("Oof!")
	    game.Workspace.BadRamp4.Material = Enum.Material.Neon --Change bad part name
	    game.Workspace.Whoops:Play()
	    wait(1)
	    game.Workspace.BadRamp4.Material = Enum.Material.SmoothPlastic --Change bad part name
	    debounce = false
    end
end)

Remember: LocalScript. Preferably in StarterPlayerScripts. Use of WaitForChild may be necessary in some cases; try reducing redundancy as well, for example, by assigning BadRamp4 to a variable.

4 Likes