You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
loop music in background, have one play 99% of the time and the other 1% of the time
What is the issue? Include screenshots / videos if possible!
music is playing once, then not playing for a while, and then playing again
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i tried to use ended:connect for the sound and then played a function, but both ways they play once, then they dont play for a while, and then they play again but i need it basically always to be looping
local commonsound = SoundService["99music"]
local raresound = SoundService["1music"]
if not commonsound.IsLoaded then
commonsound.Loaded:Wait()
end
if not raresound.IsLoaded then
raresound.Loaded:Wait()
end
commonsoundchance = 99
while true do
local randomnumber = math.random(1,99)
if (randomnumber < commonsoundchance) then
commonsound:Play()
wait(commonsound.TimeLength)
else
raresound:Play()
wait(raresound.TimeLength)
end
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
instead of using wait(commonsound.TimeLength) you could do .Ended:Wait()
also instead of check if its less than 99 you could just put ~= commonsound chance if 99 is the only number that plays that rare sound.
Is the music sound the actual length of the sound track? The audio may have no sound during the end of it. For example say the TimeLength is 30 seconds, but the sound only plays for 25 seconds you’ll have 5 seconds of no sound.
Following from @Scottifly, just check if the actual audio has no sound in it using sound.Amplitude, which returns the actual volume of a sound heard by the client, in decibels.
local RS = game:GetService("RunService")
local sound = workspace.Sound
sound:Play()
local checkAmplitude = RS.Heartbeat:Connect(function(delta) print(math.floor(sound.Amplitude)) end)
sound.Ended:Wait()
checkAmplitude:Disconnect()
print("played")
I tried this and it still waited a long time before looping again on the first go around, then once it gets going it works as intended. But this is still a problem
local commonsound = game.SoundService:WaitForChild("99music")
local raresound = game.SoundService:WaitForChild("1music")
local commonsoundchance = 99
while true do
local randomnumber = math.random(1, 99)
if randomnumber < commonsoundchance then
commonsound:Play()
task.wait(commonsound.TimeLength)
else
raresound:Play()
task.wait(raresound.TimeLength)
end
end
How about a check to see what your loading times are?
print("script started")
local commonsound = SoundService["99music"]
local raresound = SoundService["1music"]
if not commonsound.IsLoaded then
commonsound.Loaded:Wait()
print("common loaded")
end
if not raresound.IsLoaded then
raresound.Loaded:Wait()
print("rare loaded")
end
-- rest of script
local SoundService = game:GetService("SoundService")
local commonsound = SoundService["99music"]
local raresound = SoundService["1music"]
if not commonsound.IsLoaded then
commonsound.Loaded:Wait()
end
if not raresound.IsLoaded then
raresound.Loaded:Wait()
end
local commonsoundchance = 99
while true do
local randomnumber = math.random(1, 100)
if randomnumber <= commonsoundchance then
commonsound:Play()
wait(commonsound.TimeLength)
commonsound:Stop()
else
raresound:Play()
wait(raresound.TimeLength)
raresound:Stop()
end
end
thanks to everyone for the replies and help, so far nothing has worked as intended for me.
local commonsound = SoundService["99music"]
local raresound = SoundService["1music"]
commonsoundchance = 99
while true do
local randomnumber = math.random(1,99)
if (randomnumber < commonsoundchance) then
print("playing")
commonsound.Playing = true
print("trying to wait")
task.wait(commonsound.TimeLength)
print("waited successfully")
commonsound:Stop()
print("stopped succesfully")
else
raresound.Playing = true
task.wait(raresound.TimeLength)
raresound:Stop()
end
end
i first tried this script, and after it plays the first time it does goes back through and prints playing and trying to wait but the playing property of the sound doesnt change to playing. after it goes through the next wait and cycles back to the playing again it works that time.
then i tried this
local randomnumber = math.random(1,99)
if (randomnumber < commonsoundchance) then
print("playing")
commonsound:Play()
print("waiting")
task.wait(commonsound.TimeLength)
print("stopping")
commonsound:Stop()
else
raresound:Play()
task.wait(raresound.TimeLength)
raresound:Stop()
end
end
this had the same result, it prints playing and waiting but doesn’t play until the wait goes through again.
then i wanted to use .ended but im not sure how to implement it in a while true do.
before when i first made the script i had the randomizing, music determining, and playing done by a function called playmusic and then i connected this function to .ended at the bottom of the script.
this had the same result. it feels like all three of these options should work and im very confused as to why i am consistently getting 2 minutes of nothingness after the first playthrough. if anyone wants to test or see anything the sound id im using is 9046863579. it does seem important to note that originally i tested using a short clip of 3 seconds and it seemed to work perfectly but maybe that was just because i wasnt noticing the 3 seconds where it didnt run. i also tried removing the :stop in the second script but that didnt change anything.
[] is basically just a way to get a child of an object. It is an alternative to using OBJECT.Child or OBJECT:FindFirstChild(Child), with the coder instead doing OBJECT[Child].
Using OBJECT.Child does not work when the first character of the child is a number or punctuation, so you have to use either FindFirstChild or []
i fixed it. i just added a task.wait before the line where it plays the sound. this might have been a loading issue, i cant wrap my mind around exactly why it fixed it but it worked
final code:
local SoundService = game:GetService("SoundService")
local commonsound = SoundService["99music"]
local raresound = SoundService["1music"]
commonsoundchance = 99
while true do
local randomnumber = math.random(1,99)
if (randomnumber < commonsoundchance) then
task.wait(1)
commonsound:Play()
task.wait(commonsound.TimeLength)
else
task.wait(1)
raresound:Play()
task.wait(raresound.TimeLength)
end
end