Music stops after I enter a new difficulty 30% of the time

Hey so I’m having an issue with this sounds region script, sometimes it just stops after playing a few difficulties of my game and occasionally I have to rejoin for the music to start playing again. I’m not too sure what the repro steps are but if someone could analyze this script and see why this might be happening. The sound region parts aren’t touching each other nor far apart that it stops. I’ve seen this occur when I enter different difficulties and it happens 30% of the time but of course I want this to happen 0% of the time. Remember it happens when I enter a new difficulty because that’s when new music starts playing.

Script:

local SoundRegionsWorkspace = game.Workspace:WaitForChild("SoundRegions")

local SoundTable = {}

for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
	table.insert(SoundTable, game.Workspace.SoundRegions2[v.Name].Volume)
	wait(0.2)
end

local Found = false


while wait(.5) do

	for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
		local sound = game.Workspace.SoundRegions2[v.Name]

		Found = false
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))

		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())


		for _, part in pairs(parts) do
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				print("Player Was Found")
				Found = true
				break
			else
				Found = false
				print("Player Not Found")
			end
		end


		if Found == true and sound.IsPlaying == false then
			wait(0.1)
			sound:Play()
			for i = 0, SoundTable[i], 0.05 do
				sound.Volume = i
				wait(0.1)
				print"Sound Faded In"
				if sound.Volume == SoundTable[i] then
					print("Sound Faded In")
					break
				end
			end
		end


		if Found == false and sound.IsPlaying == true then
			wait(0.1)
			print("Waited")
			for i = SoundTable[i], 0, -0.05 do
				print("Fading Out")
				sound.Volume = i
				wait(0.1)
			end
			sound:Stop()
		end
	end
end

Just to clarify, are the sound objects looped?

No they aren’t, they loop on their own when you’re in the difficulty because of the script I provided.

I would recommend looping the songs, just because the “found” detection only happens the first time you hit the part (meaning the song won’t replay when it finishes). Just set all of the songs to song.Looped = true

Also, at the point in the script where you play/stop the song, to shorten the script, you can (and probably should) just use else instead of two if statements, as seen below:

if Found == true and sound.IsPlaying == false then
    --fade in/play
else
    --fade out/stop
end

Finally, this is unrelated, but a better way of fading in/out the sounds is to use TweenService. For one, this won’t require a for loop, which won’t slow down the script. You would do it like this:

        local ts = game:GetService("TweenService") --define this at top of script
        local Time = 1 --change this and define at top of script

        if Found == true and sound.IsPlaying == false then
			wait(0.1)

			sound:Play()
            ts:Create(sound, TweenInfo.new(Time), {Volume = SoundTable[i]}):Play()

			print("Faded in")
		else
			wait(0.1)

			ts:Create(sound, TweenInfo.new(Time), {Volume = 0}):Play()
            wait(Time)
			sound:Stop()

            print("Faded out")
		end

EDIT: You can also run this in a Heartbeat connection, rather than a while wait(0.5) do

game:GetService("RunService").Heartbeat:Connect(function()
    --sound stuff
end)
1 Like

Thanks for the guidance, but I think I found the repro-steps. I have to reset/die in a difficulty and then when it loops I go into another difficulty and it doesn’t play. Tested this theory 3 times and none of the 3 times did it play after I waited in a difficulty for it to loop after I died. Any quick solution for this?