Music loop script not working

You can write your topic however you want, but you need to answer these questions:

  1. 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
  2. What is the issue? Include screenshots / videos if possible!
    music is playing once, then not playing for a while, and then playing again
  3. 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.

3 Likes

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.

1 Like

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.

4 Likes

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")
1 Like

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

1 Like

according to the preview there is no blank space before or after the audio

1 Like
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

Tested and working as intended.

1 Like

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
1 Like

Is there a reason everyone is using brackets here? This should be () not [] (as far as I understand).

1 Like

I just copied it. You should probably ask @impervious42 because it’s their script, not mine.

1 Like

I’m going to go with … the way I’ve always done it.

1 Like

It shows red since SoundService is not defined in that script.

1 Like

Maybe try this code?

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
1 Like

i do have some solutions.

  1. after the wait(commonsound.TimeLength) PUT A PRINT to see if it continues the code or not
  2. you should probally use task.wait as it’s more efficient
  3. if none from the above work, just use commonsound.Ended() or more specifically use the Ended function
  4. as BoredHelperDev pointed out, u can try to stop the sounds after the wait(commonsound.TimeLength)
  5. and also when i said commonsound i also mean raresound

EDIT: i just realized someone already did point out the Ended function so yeah Obliterator pointed that out first, my bad, i didnt see it

1 Like

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.

the way i learned is brackets. it has no problem getting the sound as far as i can tell

[] 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 []

1 Like

As an example:

workspace.Part
workspace["Part"]
workspace:FindFirstChild("Part")
workspace:WaitForChild("Part", 5)
1 Like

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

That’s the reason I recommended putting prints in the top section of your script to see how long the original loading time takes.

1 Like