I have got two blocks of code that I want to incorporate as one block of code to essentially handle music and sounds playing in my game.
Code for copy + paste
-- SIMPLE BACKGROUND MUSIC SCRIPT ===============================================================================================================
local song = script.Sound
local songQueue = game:GetService("StarterGui"):WaitForChild("Sounds"):WaitForChild("BackgroundMusic"):GetChildren()
function PlayMusicQueue()
local randSong = songQueue[math.random(1,#songQueue)]
song.SoundId = randSong.SoundId
task.wait(1)
song:Play()
wait(song.TimeLength)
PlayMusicQueue()
end
PlayMusicQueue()
-- SOUND REGION SCRIPT =========================================================================================================================
local SoundRegionsWorkspace = game.Workspace:WaitForChild("SoundRegions")
local Found = false
while wait(1) do
for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
Found = false
local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))
local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
for _, part in pairs(parts) do
if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
Found = true
break
else
Found = false
end
end
if Found == true then
if script.Parent.Sounds.SoundRegions[v.Name].IsPlaying == false then
script.Parent.Sounds.SoundRegions[v.Name]:Play()
break
end
else
script.Parent.Sounds.SoundRegions[v.Name]:Stop()
end
end
end
You can see the first block is a simple background music song loop script that plays music for the player locally.
The second block of code is a simple region3 script that plays a common convenience store background song when the player enters the store.
Both are played locally.
How would I incorporate the two so that the background music pauses when the player enters the store, and then continues when they leave?
(OR is there a better way to approach it?)
I have tried mixing them but have had a few errors and issues with loops.