I have areas where different ambients should play, although even though :Play()
is called on a LocalScript, other players can also hear the sound. How should I stop this?
local ZoneModule = require(game:GetService("ReplicatedStorage").Modules.Zone)
for _, zone in pairs(workspace.World.EarthMap.MusicZones:GetChildren()) do
local Sound = Instance.new("Sound")
Sound.Parent = script.Sounds
Sound.Looped = true
Sound.Name = zone.Name
local MusicZone = ZoneModule.new(zone)
local MusicID = zone:GetAttribute("MusicID")
local MusicVolume = zone:GetAttribute("MusicVol")
Sound.SoundId = string.format("rbxassetid://%s",MusicID)
Sound.Volume = MusicVolume
MusicZone.playerEntered:Connect(function()
Sound:Play()
end)
MusicZone.playerExited:Connect(function()
fadeOut(Sound)
Sound:Stop()
end)
end
function fadeOut(Sound)
local SoundVol = Sound.Volume
repeat
Sound.Volume -= 0.1
game:GetService("RunService").Heartbeat:Wait()
until
Sound.Volume == 0
Sound.Volume = SoundVol
end