Could someone help me with a for i, v loop

So basically I have a folder under my workspace named MainStreetAreaLoop I am then adding a if v:IsA("Part") function, to make it easier to play all the audio.

What I am trying to do is a music loop, with multiple audios. Whenever I add a .Ended:Wait() function, the script breaks and will only play audio out of one audio part. When I remove it and have a v.Sound:Play() it works perfectly fine.

Could I get some help here with what I’ve done wrong? Here’s my code below:

for i,v in pairs(game.Workspace.MainStreetAreaLoop:GetChildren()) do
	if v:IsA("Part") then
		v.Dynasty:Play()
		v.Dynasty.Ended:Wait()
		v.TheresNoBusinessLikeShow:Play()
		v.TheresNoBusinessLikeShow.Ended:Wait()
		v.OpeningNight:Play()
		v.OpeningNight.Ended:Wait()
		v.Olympiades:Play()
		v.Olympiades.Ended:Wait()
		v.Enterprise:Play()
		v.Enterprise.Ended:Wait()
		v.BigShowTheme:Play()
		v.BigShowTheme.Ended:Wait()
	end
end

what is the explorer bro

for i,v in ipairs(game.Workspace.MainStreetAreaLoop:GetChildren()) do
	if v:IsA("Part") then
		v:Play()
		v.Ended:Wait()
	end
end

What’s that supposed to mean? I’m trying to reference sound within a part

Well, essentially:

  • The loops itirates through all the parts in your folder, and will begin in the first value.
  • Since the for loop will yield along the thread, :Wait() will yield the loop, and not continue looping for the next parts.

What you want to do instead is to first itirate through all parts, then itirate through the songs of these parts, and play them.

This is an abomination that I created. It should work, but I do not recommend it, lol.
local parts = workspace:WaitForChild("MainStreetAreaLoop"):GetChildren()

for _, part in parts do
    -- You don't need an index, nor do you need to use ipairs or pairs, since you're not using the index.
    if part:IsA("Part") then
        task.spawn(function()
            -- Spawns a new task for each part, so that they can play at the same time.
            while true do
                -- Loops forever, so that the music can play forever.
                for _, song in part:GetChildren() do
                    -- Gets all the children of the part, and loops through them.
                    if song:IsA("Sound") then
                        song:Play()
                        song.Ended:Wait()
                        -- Waits for the song to end, so that the next song can play.
                    end
                end
            end
        end)
    end
end

You can use :GetDescendants() instead, and put that music in a table, so all parts play the exact same sound:

Oh my god, I am terrible at coding, this is too many end statements.
local sounds = {
    [1] = "Dynasty",
    [2] = "TheresNoBusinessLikeShow",
    [3] = "OpeningNight",
    [4] = "Olympiades",
    [5] = "Enterprise",
    [6] = "BigShowTheme",
}

while true do
    for i = 1, #sounds, 1 do
        for _, instance in workspace:WaitForChild("MainStreetAreaLoop"):GetDescendants() do
            if instance:IsA("Sound") and instance.Name == sounds[i] then
                instance:Play()
                instance.Ended:Wait()
            end
        end
    end
end

I am respecting the #1 rule of programming: If it works, don’t touch it.

1 Like

Okay, i’ve tried the first code you sent, and it worked. Only issue is one of the sound parts played a different sound to the rest of the sound parts

That’s probably related to names. Try the second, I don’t know if that works.

I tried the second, it did not work. I got the first code working, only issue is once the first song played, about half way through it played the second song?

Try the following:

for i,v in pairs(game.Workspace.MainStreetAreaLoop:GetChildren()) do
	if v:IsA("Part") then
		task.spawn(function()
			v.Dynasty:Play()
			v.Dynasty.Ended:Wait()
			v.TheresNoBusinessLikeShow:Play()
			v.TheresNoBusinessLikeShow.Ended:Wait()
			v.OpeningNight:Play()
			v.OpeningNight.Ended:Wait()
			v.Olympiades:Play()
			v.Olympiades.Ended:Wait()
			v.Enterprise:Play()
			v.Enterprise.Ended:Wait()
			v.BigShowTheme:Play()
			v.BigShowTheme.Ended:Wait()
		end)
	end
end

This is unnecessary. You are looping over each descendant and comparing it to the current sounds name. FindFirstChild will do that for you for free.

local sounds = {
    [1] = "Dynasty",
    [2] = "TheresNoBusinessLikeShow",
    [3] = "OpeningNight",
    [4] = "Olympiades",
    [5] = "Enterprise",
    [6] = "BigShowTheme",
}

while true do
    for i, soundName in ipairs(sounds) do -- Using ipairs to guarantee order. Order with pairs is undefined behavior and I am not sure about the default iterator
        local lastSound -- Dirty hack, but it works!
        for _, speaker in workspace:WaitForChild("MainStreetAreaLoop"):GetChildren() do
            local sound = speaker:FindFirstChild(soundName)
            sound:Play()
            lastSound = sound
        end
        lastSound.Ended:Wait()
    end
end

NOTE: You may want to use WaitForChild instead of FindFirstChild.

Oh come on, you are not. I have seen much worse code on here than this!

tysm! it works.

how would I be able to fade down the volume of all these sounds with a button click, then fade them back up when I click the button again?

Would I run that through the same loop as before, and just tween the volume of each audio?

Yeah, I suppose so. If you need them to stop after fading ir out, you can use :Pause and :Resume to start it again from where it paused. Make sure to also use task.spawn if you’re going to use tween.Completed:Wait()

Here’s the script I’ve got:

for i,v in pairs(game.Workspace.MainStreetAreaLoop:GetChildren()) do
	if v:IsA("Part") then
		task.spawn(function()
			v.Dynasty.Fade.Disabled = false
			v.TheresNoBusinessLikeShow.Fade.Disabled = false
			v.OpeningNight.Fade.Disabled = false
			v.Olympiades.Fade.Disabled = false
			v.Enterprise.Fade.Disabled = false
			v.BigShowTheme.Fade.Disabled = false
		end)
	end
end

script.Disabled = true

What exactly is “Fade”? Is that a script? If so, you should not do that.

Instead, perform the fading operation in the main script as you did before:

Example of fade via TweenService for one sound:

local tween = TweenService:Create(sound, TweenInfo.new(2), { Volume = 0 })
tween:Play()
tween.Completed:Wait()
sound:Pause()

To resume it:

sound:Resume()
local tween = TweenService:Create(sound, TweenInfo.new(2), { Volume = 1 })
tween:Play()
1 Like

Ah awesome! So how would I do that tween for every audio in my parts?

Wrap it in a task.spawn statement and place that in a for loop

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