How exactly do I make a sound play in 1 place only from server-side?

The title is a bit misleading.

I’m trying to play a sound whenever x happens from server-side, however to my tiny knowledge I had no idea that playing sounds from a server will literally play it everywhere, regardless whether the sound is parented to X part or not.

I tried going around this via :FireAllClients(), but I don’t think that did the trick, or maybe i just did it incorrectly.

The exact thing I did was:

-- code code code
v.Parent:FindFirstChild("Humanoid"):TakeDamage(40)
event:FireAllClients(Stab:Play())
-- code code code

I have a very slight feeling this is done incredibly incorrectly, since I haven’t used an OnClientEvent anywhere.

1 Like

Why do you want the client to play the sound and not the server? Can you specify a little bit more because I cannot understand whether it is:

  • The sound should play from a certain position in the game
  • The sound should play for all players

That’s not how remote events work. A remote event is a way to send information between the client and the server. It is not a way to directly pass a function that will execute. If that’s the only thing you want, you need to add a listener for that event.

I don’t understand what “only from server side” means, so I will give you an example where it picks a random player in the server and plays the sound for him. You can read more about remote events the client-server communication on the engine documentation.

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage:FindFirstChild("RemoteEvent")

task.wait(5)
print("Getting players in server.")
local players = Players:GetPlayers()
if #players > 0 then
	local randomPlayer = players[math.random(1, #players)]
	local audioId = "rbxassetid://1844271848" -- Replace with your actual audio ID
	remoteEvent:FireClient(randomPlayer, audioId, game.Workspace)
end

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage:FindFirstChild("RemoteEvent")
remoteEvent.OnClientEvent:Connect(function(audioId, parent)
	local sound = Instance.new("Sound")
	sound.SoundId = audioId
	sound.Parent = parent
	sound:Play()
	sound.Ended:Connect(function()
		sound:Destroy()
	end)
end)

The following is only an example. Feel free to pass anything else you might need such as volume, equalizer effects, etc.
Also like @anon81993163 said, you are not very clear with your question. Playing sounds via game:GetService("SoundService"):PlayLocalSound() will make them play globally for the client that called the function. Parenting a sound to workspace has similar behaviour, it will be heard anywhere. If you need the sound to play from a certain position or point, you need to parent it to a part or attachment. More on that here.
Here is a video of how that code I showed you works, you can hear the sound on the client, but not on the server (or other clients, but watch off for respect filtering enabled.)

1 Like

I guess it is a bit confusing.

Elaboration:

The sound is parented to a part, and whenever x happens, I want the part sound to play on all clients but not globally, you know;

If you’re close enough, you can hear it, if you’re not, you can’t.

Humble attempt at drawing what I mean:

I’m aware of remote events, I just thought :FireAllClients() wouldn’t require a clientside communication.

I’m dumb, not stupid.

Even if, where would I put the client-side code? → (besides a local script) ← by that I mean where do i put the localscript, not what script I put the client code in :skull:

Just use RemoteEvent:FireClient() per player that is within range, this should sync the hearing between all players.

Or you want to give it to the client which makes it not in sync, but you will check the distance and use the said method above, passing the sound as a parameter.

there is sound propertied on the sound object: RolloffmaxDistance, which is the maximum distance any player can hear the sound, simply set that property to ur liking and play the sound from server

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.