I’m working on a game that requires tweened volume changes between SoundRegion sounds. Below is the typical SoundRegion script with some minor adjustments to make it so the volume tweens between regions rather than changing abruptly:
local plr = game.Players.LocalPlayer
local sr = workspace:WaitForChild("SoundRegions")
local ts = game:GetService("TweenService")
sr = sr:GetChildren()
local smanage = {}
for _,region in pairs(sr) do
local info = {}
local region3 = Region3.new(region.Position-(region.Size/2), region.Position+(region.Size/2))
region.Transparency = 0.8
region.CanCollide = false
info.Region = region3
info.Sound = script.SoundRegions:FindFirstChild(region.Name).Sound
table.insert(smanage,info)
end
game:GetService("RunService").RenderStepped:Connect(function()
for _,soundInfo in pairs (smanage) do
local region = soundInfo.Region
local sound = soundInfo.Sound
local parts = workspace:FindPartsInRegion3WithWhiteList(region, plr.Character:GetDescendants())
local voluptween = ts:Create(sound, TweenInfo.new(1), {Volume = 1}, true)
local voldowntween = ts:Create(sound, TweenInfo.new(1), {Volume = 0}, true)
if #parts > 0 then
if not sound.IsPlaying then
sound:Play()
voluptween:Play()
end
else
voldowntween:Play()
voldowntween.Completed:Connect(function()
sound:Stop()
end)
end
end
end)
I understand the problem lies within the last few lines, where sound:Stop()
is, but waiting for the tween to finish and then cutting the sound off sometimes leads to the sound “flickering” back on when the region is reentered (other times not, for whatever reason). I’m more making this post to ask if there is a better way to do this. Maybe I’m going about it entirely wrong, but hopefully it’s just a property I’m unaware of or a rather simple fix. Thanks in advance!