So I'm trying to create a simple music system for my game, here's how it works.
local Sound1 = script.Parent.Sound1 local Sound2 = script.Parent.Sound2 local Sound3 = script.Parent.Sound3 while wait(1) do --Plays music local Music = math.random(1,3) if Sound1.Playing == false and Sound2.Playing == false and Sound3.Playing == false then --if theres no music playing then if Music == 1 then Sound1:Play() if Music == 2 then Sound2:Play() if Music == 3 then Sound3:Play() end end end end end
First, I got all the "Sound" values. Then I made a while wait() loop. I then did math.random to randomize which music would play (1,3). I then made an "if" statement saying that if the music isn't playing then use the math.random values that were originally created in order to figure out which sound to play. Please no useless comments, anything simple like "else" statements I've already tried, so no one word can fix it. Thank you!
While this could work, I wouldn't recommend it, we can rework it to be much more efficient and nice looking.
First of all lets only use one sound object and instead store the id's of your sounds in a table
local soundObj = script.Sound local sounds = {1234567, 2345671, 3456712}
Much better, now lets set up the rest.
You can listen for certain properties of an object to change with:
instead of running code every second we can use this to listen for when the sound stops playing.
soundObj:GetPropertyChangedSignal("Playing"):Connect(function() if not soundObj.IsPlaying then end end)
this runs code whenever the Playing
property of soundObj
changes. Just in case I added a check for whether IsPlaying
is false like we want it. (not soundObj.IsPlaying
is just a shorter way to write soundObj.IsPlaying == false
)
next lets get our random sound. We'll index our table we created with a random number to get a random sound id.
local id = sounds[math.random(1, #sounds)]
so we index the sounds
table by using []
then we create a random number in the range of 1 to however long the sounds
table is (# gets the length of the adjacent table).
lastly we set the SoundId
of the soundObj and play it, then we compile it all together.
local soundObj = script.Sound local sounds = {1234567, 2345671, 3456712} soundObj:GetPropertyChangedSignal("Playing"):Connect(function() if not soundObj.IsPlaying then local id = sounds[math.random(1, #sounds)] soundObj.SoundId = id soundObj:Play() end end)
Haven't tested this yet, you might need to play around with it yourself
Hope this was coherent enough, if you have any questions I can answer them.
I forgot to mention that you need to add a Content ID when modifying SoundId from a script. Like so:
soundObj.SoundId = "http://www.roblox.com/asset/?id=" .. id -- or soundObj.SoundId = "rbxassetid://" .. id
so uh from what i've seen, you not doing it correctly.
here's what you should do instead.
local Sound1 = script.Parent.Sound1 local Sound2 = script.Parent.Sound2 local Sound3 = script.Parent.Sound3 local t = {Sound1, Sound2, Sound3} while wait() do local Music = Random.new():NextInteger(1, 3) -- you don't have to change this I just like using random.new more for _,v in pairs(t) do v:Stop() -- stops all music end t[Music]:Play() wait(t[Music].TimeLength) -- in case the sound is longer than 1 second, if it isn't, you can just hard-code it since using timelength is a bit inconsistent. end
Its a simple solution. Turn your
while wait(1) do
into
while true do
just use this bro https://www.roblox.com/library/3956471383/Song-Shuffle-Script it works the best and is ez to work with