Hi,
I made a working region sound handler but I am having problems trying to make the music continue if they leave and go back in the region fast.
Here’s my current script:
--[[
Controls music elements..
for example: inside main menu -> play menu music.
inside store -> play store music
TODO: Handle ambiance if any group has a sound named "Background"
--]]
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Assets = ReplicatedStorage.Assets
local Packages = ReplicatedStorage.Packages
local Modules = ReplicatedStorage.Source.Modules
local Util = Modules.Util
local Knit = require(Packages.Knit)
local RichTextMarkup = require(Modules.RichText)
local Regions = {}
local Blacklist = {"Ambience", "Ambiance"}
local RegionController = Knit.CreateController{
Name = "RegionController";
}
function createRegions(Folder: Folder)
for _, region in pairs(Folder:GetChildren()) do
local info = {}
local region3 = Region3.new(region.Position - (region.Size/2),region.Position + (region.Size/2))
region.Transparency = 1
info.Region = region3
info.SoundGroup = SoundService:FindFirstChild(region.name)
table.insert(Regions,info)
end
end
function createRegionUi(Name: string, Animation: string)
if Players.LocalPlayer.PlayerGui:FindFirstChild("RegionPreface") then
return
end
local RegionUI = Assets.RegionPreface:Clone()
local RegionFrame = RegionUI.Dialogue.TextFrame
local defaultIn = "<TextScale=1><AnimateStepFrequency=1><AnimateStyleTime=0.5>".."<AnimateStyle="..Animation..">"
local defaultOut = "<TextScale=/><AnimateStepFrequency=/><AnimateStyleTime=/><AnimateStyle=/>"
local Text = defaultIn.. Name ..defaultOut
local TextObject = RichTextMarkup:New(RegionFrame, Text, {Font = "Cartoon"})
RegionUI.Dialogue.Position = UDim2.new(0.5,0,-0.12,0)
RegionUI.Parent = Players.LocalPlayer.PlayerGui
local tweenShadowIn = TweenService:Create(
RegionUI.Dialogue,
TweenInfo.new(0.6),
{Position = UDim2.new(0.5,0,0.022,0)})
local tweenShadowOut = TweenService:Create(
RegionUI.Dialogue,
TweenInfo.new(0.6),
{Position = UDim2.new(0.5,0,-0.12,0)})
task.spawn(function()
TextObject:Animate()
tweenShadowIn:Play()
tweenShadowIn.Completed:Connect(function(playbackState: Enum.PlaybackState)
task.wait(2)
tweenShadowOut:Play()
end)
tweenShadowOut.Completed:Connect(function()
RegionUI:Destroy()
end)
end)
end
function update(deltaTime)
-- TODO: check if player is inside a region
for _, infoTable in pairs(Regions) do
local Region = infoTable.Region
local SoundGroup = infoTable.SoundGroup
local Parts = workspace:FindPartsInRegion3WithWhiteList(Region, Players.LocalPlayer.Character:GetDescendants())
local Thread = function()
for i, Song: Sound in pairs(SoundGroup:GetChildren()) do
if Song.IsPlaying and Song ~= "Ambience" then
return
end
if table.find(Blacklist, Song.Name) ~= nil then
continue
end
local fadeinTween = TweenService:Create(Song, TweenInfo.new(0.6) , { Volume = 1 })
Song:Play()
fadeinTween:Play()
createRegionUi(SoundGroup.Name, "Fade")
Song.Ended:Wait()
end
end
if #Parts > 0 then -- if detect player inside
task.defer(Thread)
if SoundGroup:FindFirstChild("Ambience") then
print("Found ambience, playing..")
SoundGroup:FindFirstChild("Ambience"):Play()
end
else -- if not detect
for i, Song: Sound in pairs(SoundGroup:GetChildren()) do
if Song.IsPlaying then
local fadeOutTween = TweenService:Create(Song, TweenInfo.new(0.6) , { Volume = 0 })
fadeOutTween:Play()
fadeOutTween.Completed:Connect(function(playbackState: Enum.PlaybackState)
Song:Pause()
end)
end
end
if SoundGroup:FindFirstChild("Ambience") then
SoundGroup:FindFirstChild("Ambience"):Stop()
end
end
end
end
createRegions(Assets.Regions)
RunService.RenderStepped:Connect(update)
return RegionController