Client sided sound doesn't always play

I am trying to replicate the gun fire sound to all the clients because on the server it is delayed and I have done that but sometimes the sound doesn’t even play and sometimes it does. in-game but in studio this problem never happens.

Server:

Tool.ReplicateClient:FireAllClients(Tool)

Client:

function PlayFireSound(weapon)
	local NewSound = weapon.Handle.Fire:Clone()
	NewSound.Parent = weapon.Handle
	NewSound:Play()
	game:GetService("Debris"):AddItem(NewSound, NewSound.TimeLength)
end


Tool.ReplicateClient.OnClientEvent:Connect(function(weapon)
	PlayFireSound(weapon)
	weapon.Handle.Flare.ParticleEmitter:Emit(100)
end)

Anyone know how to fix it?

2 Likes

Try this:

function PlayFireSound(weapon)
	local NewSound = weapon.Handle.Fire:Clone()
	NewSound.Parent = weapon.Handle
    NewSound.Loaded:Wait() -- load the sound before playing
	NewSound:Play()
	game:GetService("Debris"):AddItem(NewSound, NewSound.TimeLength)
end


Tool.ReplicateClient.OnClientEvent:Connect(function(weapon)
	PlayFireSound(weapon)
	weapon.Handle.Flare.ParticleEmitter:Emit(100)
end)
1 Like

Try adding some warnings:

function PlayFireSound(weapon)

	local NewSound = weapon.Handle.Fire

	if NewSound then
		local NewSoundClone = NewSound:Clone()
		NewSoundClone.Parent = weapon.Handle
		NewSoundClone:Play()
		game:GetService("Debris"):AddItem(NewSoundClone, NewSoundClone.TimeLength)
	else
		warn("Fire sound not found.", weapon)
	end
end


Tool.ReplicateClient.OnClientEvent:Connect(function(weapon)
	
	if weapon then
		PlayFireSound(weapon)
		weapon.Handle.Flare.ParticleEmitter:Emit(100)
	else
		warn("Weapon not found.")
	end
	
end)

Then check your dev panel while playing game.

1 Like

Store the sound in the Handle of the gun so it doesn’t have to be created, just Play() it.
That’s probably what’s causing the issue.

In Studio all of your scripts run on your computer so there isn’t a Server and you won’t see any delay.

The sound needs to be cloned for automatic weapons.

Otherwise it wont play each time a bullet it fired.

1 Like

Are you using an unreliableRemoteEvent I think it should be obvious by the name but they don’t always make their way to/from the server.

After using that the sound only plays once and then doesn’t fire again

No warnings show up in the dev panel

Nope its a normal remotevent I am not using any unreliable remote events

1 Like

I just realised that even though it fires all the clients its still only seeable by the player that fired the gun so could that maybe be why and how would I fix it?

that would probably be because other clients don’t have the local script. For the sound try using sound.Ended:Once() or sound.Ended:Wait() just to make sure the sound has loaded. Furthermore it’s not the best idea to send a server to client request just to play a client sided sound, you’d be better off doing that on the server or playing it on the client before sending to server

Can’t you technically just… play the sounds on the server?

When i want to play a sound that should only be played on the client via a remote event, i usually just create a remote event that uses the Sound’s Name or a Sound ID, but if everyone should hear the sound, let the server handle it.

It’s normal for a server to be delayed, no connections are perfect, I’ve had a hitbox system once which was weirdly offset client-side, i checked the server view and it perfectly followed the player since the hitboxes were mostly server-side. Maybe it’s the same deal with the sounds?

They make the sound even more broken

It is delayed because of client-server delay

I have seemed to fix the issue by replacing

game:GetService("Debris"):AddItem(NewSoundClone, NewSoundClone.TimeLength)

To:

NewSoundClone.Ended:Connect(function()
	NewSoundClone:Destroy()
end)

So it was a problem with the Debris service