Music Script Not Working Properly (Need fixed)

WIS;TD: Every time you click a flague, if you previously clicked one the music loop would close and create a new one, and over and over again

Problem: When you click a flag more than once, the loops don’t close and the wait times are messed up, sometimes a music plays for 2 seconds and goes to another one, sometimes it plays a music that does not belong to those flagues

Game: carsg.rbxl (347.2 KB)

Repro: Click the settings icon, scroll to the music section and change the flague about every 5 seconds for 3-5 times, wait, look at the output it will print out the time length, use your observational skills to see if it doesn’t match that wait times, additionally you can select the sound object and see the timeposition, if it is not max but it changes sound, that means you have seen the bug in my code. Now, I need a fix for this ASAP. Thanks

Hierarchy:
image

Script (Main):

   wait(1)
    local loo = true
    script.Parent.Value.Event.Event:Connect(function()
    	print("a")
    	loo = false
    	script.Parent.Sound:Stop()
    	loo = true
    	repeat 
    		if script.Parent.Value.Value=="1" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.american.Sound.SoundId
    		elseif script.Parent.Value.Value=="2" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.chinese.Sound.SoundId
    		elseif script.Parent.Value.Value=="3" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.japanese.Sound.SoundId
    		elseif script.Parent.Value.Value=="4" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.indian.Sound.SoundId
    		elseif script.Parent.Value.Value=="5" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.easteuropean.Sound.SoundId
    		elseif script.Parent.Value.Value=="6" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.soviet.Sound.SoundId
    		elseif script.Parent.Value.Value=="7" then
    			script.Parent.Sound.SoundId = game.ReplicatedStorage.music.british.Sound.SoundId
    		end
    		repeat wait() until script.Parent.Sound.IsLoaded==true
    		script.Parent.Sound:Play()
    		print(script.Parent.Sound.TimeLength)
    		wait(script.Parent.Sound.TimeLength )
    	until loo==false
    end)
2 Likes

Hello!
This issue seems to occur because you over time generate more and more running loops as you change your music more and more. You seem to have tried fixing this by implementing the variable loo, however it only checks for that statement every 30-120 seconds due to the wait() call before it.

If I were to fix this issue, I would definitely utilize the “Looped” property of Sound instances. With this property enabled, you’ll experience the same looping as your current script tries to imitate. With that property enabled, you should now be able to delete the looping part of your code alongside the wait() call and loo variable. You’re also referencing script.Parent.Value and script.Parent.Sound a lot, so I’d consider defining them as variables.

Good luck with your game!

Could you please rephrase your solution? I believe using your solution aah… I don’t know how to word it properly, but basically it’s supposed to work like categories, like a never ending looped playlist of different songs. There are multiple music in each flague or category, so deleting the looping part of my code and turning on the looped property would make it play the same music over and over again

Please do not repost threads.
https://devforum.roblox.com/t/music-script-isnt-working-properly-need-fixed/543534

1 Like
wait(1)
script.Parent.Sound.SoundId = game.ReplicatedStorage.music.american.Sound.SoundId
script.Parent.Sound:Play()
script.Parent.Sound.Ended:Connect(function()
	if script.Parent.Value.Value=="1" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.american.Sound.SoundId
		script.Parent.Sound:Play()
	elseif script.Parent.Value.Value=="2" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.chinese.Sound.SoundId
		script.Parent.Sound:Play()
	elseif script.Parent.Value.Value=="3" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.japanese.Sound.SoundId
		script.Parent.Sound:Play()
	elseif script.Parent.Value.Value=="4" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.indian.Sound.SoundId
		script.Parent.Sound:Play()
	elseif script.Parent.Value.Value=="5" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.easteuropean.Sound.SoundId
		script.Parent.Sound:Play()
	elseif script.Parent.Value.Value=="6" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.soviet.Sound.SoundId
		script.Parent.Sound:Play()
	elseif script.Parent.Value.Value=="7" then
		script.Parent.Sound.SoundId = game.ReplicatedStorage.music.british.Sound.SoundId
		script.Parent.Sound:Play()
	end
end)
script.Parent.Value.Event.Event:Connect(function()
	script.Parent.Sound:Stop()
end)

Help me please. Someone on the other post told me to use the event .ended but it doesn’t seem to work