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?