I made a simple one that just plays one track (with the sound added in workspace) and the localscript:
game.Workspace.Sound:Play()
Since players request more sounds, I tried this to add a few more in, but it didn't work out. I did also add the sounds and changed the sound name to Sound2, Sound3, etc.
game.Workspace.Sound:Play() wait(141) --length of sound one game.Workspace.Sound2:Play() wait(114) --length of sound two game.Workspace.Sound3:Play() wait(75.5) --length of sound three repeat
Any suggestions? Thanks.
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 --code goes here 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!
I just organized your code and added a loop
local SoundOne = game.Workspace.Sound local SoundTwo = game.Workspace.Sound2 local SoundThree = game.Workspace.Sound3 while wait() do SoundOne:Play() wait(141) -- or wait(SoundOne.TimeLength) SoundTwo:Play() wait(114) -- or wait(SoundTwo.TimeLength) SoundThree:Play() wait(75.5) -- or wait(SoundThree.TimeLength)