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.
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)
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.
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.