Issue with creating a proper sound system that replicates sounds to all clients

Hi, I’m attempting to make a sound system for my weapons system, here’s what I got so far.

  • This is the function that plays the sound effect, what it does is it creates another sound instance and adds it to handle of the tool, then plays it for the local player, after that it will delete it with debris, and then fire to the server.
local function PlaySound(SoundEffect)
	local SoundInstance = SoundsFolder[SoundEffect]
	local SoundClone = SoundInstance:Clone()
	SoundClone.Parent = Handle
	SoundClone:Play()
	
	Debris:AddItem(SoundClone, SoundClone.TimeLength)
	PlaySoundEvent:FireServer(SoundInstance, SoundClone.TimeLength)
end
  • On the server here, it will simply fire all information back to the client again.
local function PlaySound(Player, SoundInstance, debrisTime)
	print(SoundInstance)
	PlaySoundEvent:FireAllClients(Player, SoundInstance, debrisTime)
end

PlaySoundEvent.OnServerEvent:Connect(PlaySound)
  • In another client script, on a client event it will call this function, essentially doing the same thing as the other client script, but ignoring the player that fired it.
local function PlaySound(Plyr, SoundInstance, debrisTime)
		if Player.Name ~= Plyr.Name then
		local SoundClone = SoundInstance:Clone()
		local SoundParent = SoundInstance.Parent.Parent
		SoundClone.Parent = SoundParent
		SoundClone:Play()

		Debris:AddItem(SoundClone, debrisTime)
	end
end
PlaySoundEvent.OnClientEvent:Connect(PlaySound)

The reason why I’m creating this system is because I cannot simply play it on the server because it will cause a double sound to play for the player who uses the weapon, and if I just play it on the server it will be delayed.

The problem I am having

I don’t really know how to stop the sound if needed, like for example if somebody reloads a gun, but then unequips the gun mid reload, I’d want the sound effect to stop, which I can do for the player who reloads it easily, but the issue is other players still hear the sound, and because the Tool has been unequipped, the instance of the sound always comes back as nil when I attempt to do anything, so I am really trying to figure out how to solve this.

> Does anybody have any solutions?

I think you have a few solutions here but first you need to decide the behaviour that you expect.

What do you expect to happen when the gun that fires is reloaded? Do you think the shooting sound should stop?

I am assuming your issue is the sound being nil when the gun is unequipped. One easy solution to this problem is to associate each player, with the last sound instance played (on the server). That means that if you want to stop it, you don’t need to pass the instance, instead you can pick it from the table.

I am not sure how I’d set that up really, is there any way I can get the Instance?

On the client script you can do this:

local lastPlayedSounds = {}

local function PlaySound(Plyr, SoundInstance, debrisTime)
		if Player.Name ~= Plyr.Name then
		local SoundClone = SoundInstance:Clone()
		local SoundParent = SoundInstance.Parent.Parent
		SoundClone.Parent = SoundParent
        lastPlayedSounds[Player.UserId] = SoundClone
		SoundClone:Play()

		Debris:AddItem(SoundClone, debrisTime)
	end
end
PlaySoundEvent.OnClientEvent:Connect(PlaySound)

local function StopSound(Player)
    local SoundInstance = lastPlayedSounds[Player.UserId]
    if not SoundInstance then return end
    lastPlayedSounds[Player.UserId] = nil
    SoundInstance:Stop()
    SoundInstance:Destroy()
end

PlaySoundEvent.OnServerEvent:Connect(PlaySound)

You will need to create equivalent events and server function for the stopSound method.

1 Like

Omg thank you so much, I’ve been trying to figure this out for hours and you just saved me, it works flawlessly and I really appreciate the help!

1 Like

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