this script is broken. its supposed to play a lot of songs in a zone and turn by turn.
local Players = game:GetService("Players")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local SoundRegions = workspace:WaitForChild("SoundRegions")
local RegionSounds = SoundService:WaitForChild("RegionSounds")
-- For each region, provide a list of sound names (songs)
local RegionSongs = {
["MusicPlayer Union"] = {"Humming5", "Humming", "Humming2", "Humming4", "Humming3"}
}
local Tween = function (Object, Time, Style, Direction, Property)
return TweenService:Create(Object, TweenInfo.new(Time, Enum.EasingStyle[Style], Enum.EasingDirection[Direction]), Property)
end
-- Helper to fade out and stop all sounds
local function FadeOutAllSounds(exceptSound)
for _, Sound in RegionSounds:GetChildren() do
if Sound ~= exceptSound and Sound.IsPlaying then
task.spawn(function()
local VolumeTween = Tween(Sound, 1, "Quad", "Out", {Volume = 0})
VolumeTween:Play()
VolumeTween.Completed:Wait()
Sound:Stop()
end)
end
end
end
-- Playback state per region
local RegionPlayback = {}
-- Helper to play next song in region (random or sequential)
local function PlayNextSong(regionName, sequential)
local songs = RegionSongs[regionName]
if not songs or #songs == 0 then return end
-- Get playback state
local state = RegionPlayback[regionName]
if not state then
state = {currentIndex = 1}
RegionPlayback[regionName] = state
end
local nextIndex
if sequential then
nextIndex = state.currentIndex
state.currentIndex = state.currentIndex + 1
if state.currentIndex > #songs then
state.currentIndex = 1
end
else
nextIndex = math.random(1, #songs)
end
local soundName = songs[nextIndex]
local sound = RegionSounds:FindFirstChild(soundName)
if not sound then return end
-- Fade out other sounds
FadeOutAllSounds(sound)
-- Fade in this sound
if sound.Volume < 0.25 then
local VolumeTween = Tween(sound, 1, "Quad", "Out", {Volume = 0.25})
VolumeTween:Play()
end
sound:Play()
-- Connect Ended event to play next song
if state.connection then
state.connection:Disconnect()
end
state.connection = sound.Ended:Connect(function()
PlayNextSong(regionName, sequential)
end)
end
-- Regions setup
local Regions = {}
for regionName, songs in RegionSongs do
local part = SoundRegions:FindFirstChild(regionName)
if part then
Regions[regionName] = {
Part = part,
Songs = songs
}
end
end
local OverlapParameters = OverlapParams.new()
OverlapParameters.FilterType = Enum.RaycastFilterType.Include
OverlapParameters.MaxParts = 1
local currentRegion = nil
while task.wait(1) do
-- Always update the filter to the current character
local character = Player.Character or Player.CharacterAdded:Wait()
OverlapParameters.FilterDescendantsInstances = {character}
local foundRegion = nil
for regionName, regionData in Regions do
local Parts = workspace:GetPartsInPart(regionData.Part, OverlapParameters)
if #Parts > 0 then
foundRegion = regionName
break
end
end
if foundRegion then
if currentRegion ~= foundRegion then
-- Entered a new region, start playback
currentRegion = foundRegion
-- Play next song (set sequential=false for random, true for sequential)
PlayNextSong(currentRegion, false) -- Change to true for sequential playback
end
else
if currentRegion then
-- Left region, fade out all sounds and disconnect Ended event
FadeOutAllSounds(nil)
local state = RegionPlayback[currentRegion]
if state and state.connection then
state.connection:Disconnect()
state.connection = nil
end
currentRegion = nil
end
end
end