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
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.
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?
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!
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()
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