Sound areas bugged after song fading

Hi! I’m making sound areas for a game and want the song to fade as you leave, for some reason whenever you leave it fades as normal but doesn’t allow new songs to play, or for songs to restart can someone help me?

local plr = game.Players.LocalPlayer

local soundRegions = workspace:WaitForChild("SoundRegions")
soundRegions = soundRegions:GetChildren()

local soundManagement = {}

for _,region in pairs(soundRegions) do

	local info = {}

	local region3 = Region3.new(region.Position-(region.Size/2),region.Position+(region.Size/2))
	region.Transparency = 1

	info.Region = region3
	info.Sound = script.SoundRegions:FindFirstChild(region.name).Sound

	table.insert(soundManagement,info)

end

game:GetService("RunService").RenderStepped:Connect(function()

	for _,soundInfo in pairs(soundManagement) do

		local region = soundInfo.Region
		local sound = soundInfo.Sound
		local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())

		if #parts > 0 then

			if not sound.IsPlaying then

				sound:Play()

			end

		else
			
			local volume = sound.Volume
			for i = volume, 0, -.05 do
				wait(.1)
				sound.Volume = sound.Volume - .05
				print(sound.Volume)
			end
			
			sound:Stop()

		end

	end

end)
local volume = sound.Volume
			for i = volume, 0, -.05 do
				wait(.1)
				sound.Volume = sound.Volume - .05
				print(sound.Volume)
			end

This was my original solution, but it doesn’t work.

1 Like

Are you trying to make the sound fade away and fade in? cause the code seems fine until you try your original solution

I would like it to fade in and out yes, that code was for fading out but I wanted to make it fade in aswell.

1 Like

Try this [localscript in startercharacterscripts]

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
plr.CharacterAdded:Connect(function()
	char=plr.Character
end)

local rs = game:GetService("RunService")
local ts = game:GetService("TweenService")

local info = TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)

local regions = {}

for i,v in pairs(workspace.Regions:GetChildren()) do
	tab = {
		["CFrame"] = v.CFrame,
		["Sound"] = v.Sound,
		["Active"] = false,
		["Size"] = v.Size,
		["Volume"] = v.Sound.Volume
	}
	tab.Sound.Looped=true
	tab.Sound.Volume=0
	tab.Sound:Play()
	tab.Sound.Parent=game.SoundService
	table.insert(regions,tab)
end
workspace.Regions:Destroy()

function fade(sound,vol)
	local goal = {}
	goal.Volume= vol
	local tween = ts:Create(sound,info,goal)
	tween:Play()
end

function checkisinregion()
	for i,v in pairs(regions) do
		local ovlapparams = OverlapParams.new()
		ovlapparams.FilterType= Enum.RaycastFilterType.Whitelist
		ovlapparams.FilterDescendantsInstances= {char}
		local found = #workspace:GetPartBoundsInBox(v.CFrame,v.Size,ovlapparams)>0 and true or false
		if found then
			if not v.Active then
				v.Active=true
				fade(v.Sound,v.Volume)
			end
		else
			if v.Active then
				v.Active=false
				fade(v.Sound,0)
			end
		end
	end
end

rs.RenderStepped:Connect(function()
	checkisinregion()
end)

and add a folder called regions in workspace with a sound this should work
image

1 Like

So did it work? Also, if it did Please tap on the solution button on my post to help others and help me

1 Like

Sorry for taking so long to reply! I had to deal with something personal, thank you it’s working now!

1 Like