Script help Sound Zone Loop multiple sounds

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


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")

-- FIX: Each region gets a LIST of sounds
local RegionNames = {
	["MusicPlayer Union"] = {
		"Humming",
		"Humming2",
		"Humming3",
		"Humming4",
		"Humming5"
	}
}

local Tween = function(Object, Time, Style, Direction, Property)
	return TweenService:Create(
		Object,
		TweenInfo.new(Time, Enum.EasingStyle[Style], Enum.EasingDirection[Direction]),
		Property
	)
end

-- Convert region names + sound lists into usable data
local Regions = {}
for regionName, soundList in next, RegionNames do
	Regions[regionName] = {
		Part = SoundRegions:WaitForChild(regionName),
		Sounds = {}
	}

	for _, soundName in ipairs(soundList) do
		table.insert(Regions[regionName].Sounds, RegionSounds:WaitForChild(soundName))
	end
end

local OverlapParameters = OverlapParams.new()
OverlapParameters.FilterDescendantsInstances = {Player.Character}
OverlapParameters.FilterType = Enum.RaycastFilterType.Include
OverlapParameters.MaxParts = 1

local currentSound = nil

while task.wait(1) do
	local inRegion = false

	for regionName, regionData in next, Regions do
		local parts = workspace:GetPartsInPart(regionData.Part, OverlapParameters)

		if #parts > 0 then
			inRegion = true

			-- pick a random sound if none is active
			if not currentSound or not currentSound.IsPlaying then
				local randomSound = regionData.Sounds[math.random(1, #regionData.Sounds)]
				currentSound = randomSound

				-- stop all other sounds
				for _, s in next, regionData.Sounds do
					if s ~= currentSound and s.IsPlaying then
						local tween = Tween(s, 1, "Quad", "Out", {Volume = 0})
						tween:Play()
						tween.Completed:Wait()
						s:Stop()
					end
				end

				-- fade in the new sound
				currentSound.Volume = 0
				currentSound:Play()
				Tween(currentSound, 1, "Quad", "Out", {Volume = 0.25}):Play()
			end
		end
	end

	if not inRegion then
		if currentSound then
			local tween = Tween(currentSound, 1, "Quad", "Out", {Volume = 0})
			tween:Play()
			tween.Completed:Wait()
			currentSound:Stop()
			currentSound = nil
		end
	end
end

Code fixed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.