From what I can tell your problem is that you're not waiting for the sounds to load.
To make sure the item you're requesting has loaded it's important to use WaitForChild.
I believe that was your problem. The rest is just optimization.
What your code is doing will work but it could get annoying adding the sound over and over again. Using a Loop, like a While True Do loop, could help with this problem.
To gain access to some of the sound events and such, I would suggest using alocal script. Now when using a local script it's important to remember to put it in the correct places. Local Scripts Do Not work in Workspace or ServerScriptService. You can place Local scripts here for replication in the future however. But for what I would suggest, you should place the local script in StarterPack. Anything you place in the StarterPack will replicate to the player. You could place the LocalScript in other places so long as it replicates to the player.
For simplicity, we will be putting all of your sounds in a table for easy access. Make sure to use WaitForChild(). Like so,
local SoundTable =
{
game.Workspace:WaitForChild("Pitch1"),
game.Workspace:WaitForChild("Pitch2"),
game.Workspace:WaitForChild("Pitch3")
}
All you have to do with this setup if you want to add more sounds is to simply add them under the others. No more work needed.
Now, to make the sounds loop, I'll be using a while true do loop. These are the most simple. It will run through all of the code inside the loop Forever. until broken with a break. We wont be using a break however, because we want the sounds to loop forever. Here's more on loops. Now here's what the loop would look like,
local SoundTable =
{
game.Workspace:WaitForChild("Pitch1"),
game.Workspace:WaitForChild("Pitch2"),
game.Workspace:WaitForChild("Pitch3")
}
while true do
end
I wont go too far into detail about how tables work. Here's a link to the wiki for that. But basically we will be using TableName[ItemNumber] to be accessing the sounds. So say we wanted to access the second item in our table, we would use SoundTable[2].
We will also now be using a for loop. This loops through code for an amount of times. We want it to loop through our code for the number of items in our table. To do this we will be using the #. So lets say we did print(#SoundTable) it would print 3. Because there are 3 items in our table.
Here's what our code would look like now,
local SoundTable =
{
game.Workspace:WaitForChild("Pitch1"),
game.Workspace:WaitForChild("Pitch2"),
game.Workspace:WaitForChild("Pitch3")
}
while true do
for i = 1, #SoundTable do
SoundTable[i]:Play()
end
end
This almost works. However, we need to wait for the sound to end. To do this, we can wait for the event Ended to fire. To do this, we do the following,
local SoundTable =
{
game.Workspace:WaitForChild("Pitch1"),
game.Workspace:WaitForChild("Pitch2"),
game.Workspace:WaitForChild("Pitch3")
}
while true do
for i = 1, #SoundTable do
SoundTable[i]:Play()
SoundTable[i].Ended:wait()
end
end
Hope that helped!
Good Luck!
1 comment
Ah, my bold and highlight random words phase.