Server to Clients sound doesn't work

Initially sounds were played server side, but I noticed under high ping, there would be a delay in the sound playing for the player who fired their gun.
Instead, I want the client to play the sound so they’ll receive feedback immediately, while the server handles playing the sound to everyone else.

local

remotePlaySound.OnClientEvent:Connect(function(soundId, source)
	local serverSound = Instance.new("Sound")
	serverSound.SoundId = soundId
	serverSound.Parent = workspace
	serverSound:Play()

	serverSound.Ended:Wait()
	serverSound:Destroy()
end)

function PlaySound(sound)
	sound:Play()
	remotePlaySound:FireServer(sound.SoundId, toolModel.Source)
end

server

remotePlaySound.OnServerEvent:Connect(function(player, soundId, source)
	
	-- play sound to all other players
	for _, otherPlayer in pairs(Players:GetPlayers()) do
		if player ~= otherPlayer then
			remotePlaySound:FireClient(otherPlayer, soundId, source)
		end
	end
end)

The sound is created at the source, however the other player still can’t hear it for some reason.
image

I tried including the player who fired inside of the FireClient loop. The sound only plays for the player who fired, not for anyone else.

local

remotePlaySound.OnClientEvent:Connect(function(source, soundName)
	local sound = source:FindFirstChild(soundName)
	sound:Play()
end)

function PlaySound(sound)
	remotePlaySound:FireServer(toolModel.Source)
end

server

remotePlaySound.OnServerEvent:Connect(function(player, source)
	for _, Player in pairs(Players:GetPlayers()) do
		remotePlaySound:FireClient(Player , source, "Fire")
	end
end)

there’s a property inside of SoundService called RespectFilteringEnabled

what this property does, is any sound played from a LocalScript is automatically played globally, not just locally

there is honestly no point in playing the sound seperately on the initiator and the server, just play the sound on the server

playing sounds via remote events is kind of bad, because any receiver with higher ping will still have that ping delay,

so yeah, just play sounds on the server

image

Regardless, the player should have immediate feedback when they fire a gun, otherwise it doesn’t feel right. I’ve tested it and even with relatively minor lag (~500ms ping) the delay in sound is unacceptable.

i don’t think 500 ping is minor

then my soundservice suggestion still applies, if not you could always use two seperate sounds for client and server

The cause for the delay could be because of the lines:

serverSound.Ended:Wait()
	serverSound:Destroy()

To fix this, you might want to use a coroutine or a task.spawn function.

Exp:

task.spawn(function()
serverSound.Ended:Wait()
	serverSound:Destroy()
end)

The cause in delay is because I changed the incoming replication lag in studio settings, which I set on purpose to test the game with high ping.
This is not the problem, the problem is the sound doesn’t play on the client when the server fires the remote event.

Turns out everything in my code does work. It’s just Roblox Studio doesn’t play any sound when you click off the window. Would’ve been nice if someone told me this.

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