Song Randomizer Sometimes stops or changes Mid-Song

hi developers! i have a pretty weird bug for my randomizing song picker. i use tables for my song picker because it was neat. for some reason, there’s a chance of the song to randomly stop or to start a new song but the song starts in the middle. i previously used task.wait(script.Parent.TimeLength) but it wasn’t great so i had to switch to script.Parent.Ended:Wait(). any help appreciated! :innocent:

(note: the script is in the sound)

local soundIDS = {
	17549220287,
	17549225156,
	17549226726,
	17549221036,
	17549221930,
	17549226133,
	17549223475,
	18821187061,
	18821267117,
	117141723380409,
	73618185254458,
	107435837994770
}

while task.wait() do
	local randomSound = soundIDS[math.random(1, #soundIDS)]
	script.Parent.SoundId = "rbxassetid://"..randomSound
		
	script.Parent.TimePosition = 0
	
	script.Parent:Play()
	
	script.Parent.Ended:Wait()
	
	repeat task.wait(3)
		local randomNumberPlay = math.random(1, 5)
	until randomNumberPlay == 5
end
5 Likes

That’s because you used

This makes the script chooses random number between 1 and 5 every 3 seconds when the previous song has ended. It will ONLY repeat the loop when it gets the number 5. So when you feel like the song is stopping, it’s actually the script looping trying to get the number 5. It’s not a bug, it just how the script is intended to run.

I assume you are making a system that will play a random song when the previous song ended right? If so then you might want to change the script to:

local soundIDS = {
	17549220287,
	17549225156,
	17549226726,
	17549221036,
	17549221930,
	17549226133,
	17549223475,
	18821187061,
	18821267117,
	117141723380409,
	73618185254458,
	107435837994770
}

while task.wait() do
	local randomSound = soundIDS[math.random(1, #soundIDS)]
	script.Parent.SoundId = "rbxassetid://"..randomSound

	script.Parent.TimePosition = 0

	script.Parent:Play()

	script.Parent.Ended:Wait()
end

Try this and let me know if that helps!

4 Likes

hey man, i have a script from a while back that does the same thing. you can choose whatever way is comfortable for you.

for my obby game i put a folder called songs in the SoundService section in workspace with all the song ID’s attached to each sound. this is an ineffective way of doing it in case u wanted to update the songs, which is why u would use an table like u did.

script:

local songs = game:GetService("SoundService").songs --song folder in soundservice

while task.wait() do -- u have to add wait or rblx studio crashes
	for i, song in pairs(songs:GetChildren()) do --getting all the sounds from folder
		if song:IsA("Sound") then -- if its a SOUND then:
			local randomNo = math.random(1, #songs:GetChildren()) -- choosing a random number out of how many songs are in the folder
			local randomSong = songs:GetChildren()[randomNo] -- plays a random song from all the songs
			randomSong:Play() -- play song
			randomSong.Ended:Wait() -- wait until the song has finished, then it'll play another!
		end
	end
end

image

2 Likes

the repeat task.wait(3) is for the song to randomly appear. it’s like in minecraft where the song randomly appears out of nowhere. unfortunately it didn’t work.

edit: like the title, without the randomizer part, it stops or plays a new song starting from the middle. the randomizer makes it rarely happen but some people call it annoying when the song is being played.

i really don’t like having multiple audio instances and i usually like keeping it all on one instance.

1 Like

i agree, keeping everything in your game as performant as possible is important :+1:

hello!

im not 100% sure why this is happening, could just be a bug on roblox’s end.

for the sounds starting in the middle, you could try using ContentProvider:PreloadAsync(), which yields the code until all of the sounds have been loaded.

as for stopping and starting, it could be a bug with script.Parent.Ended:Wait(). ive tried using a while loop with script.Parent.IsPlaying as an alternative. accounting for both of these looks like this:

local ContentProvider = game:GetService("ContentProvider")

local soundIDS = {
	17549220287,
	17549225156,
	17549226726,
	17549221036,
	17549221930,
	17549226133,
	17549223475,
	18821187061,
	18821267117,
	117141723380409,
	73618185254458,
	107435837994770
}

ContentProvider:PreloadAsync(soundIDS) -- Preload sounds

while task.wait() do
	local randomSound = soundIDS[math.random(1, #soundIDS)]
	script.Parent.SoundId = "rbxassetid://"..randomSound
		
	script.Parent.TimePosition = 0
	
	script.Parent:Play()
	
	while script.Parent.IsPlaying do -- Instead of script.Parent.Ended:Wait()
		print(script.Parent.TimeLength - script.Parent.TimePosition)
		task.wait()
	end
	
	repeat task.wait(3)
		local randomNumberPlay = math.random(1, 5)
	until randomNumberPlay == 5
end

keep in mind that for both of these, im just going off a whim and im not actually sure if they will work. if neither do, just leave another comment and ill be happy to help

good luck!

2 Likes

thank you so much! i couldn’t find a fix for this in a month. :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.