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