Music stops after I progress into another difficulty

Hey, I’m having an issue where when you die/reset in a difficulty and you go into a new difficulty (sound regions) the music will completely stop and you’ll need to rejoin the game for the music to play again. Can anyone fix this issue?

Here’s the script to look through:

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

Well your while wait loop is scanning for the player’s character. When the player dies then there is no character for that player until the player respawns, so that script would get an error because there is no game.Players.LocalPlayer.Character. I am not sure if player:FindFirstChild(“Character”) works, but if it does then this would work:

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 character = game.Players.LocalPlayer:FindFirstChild("Character") 
                   if character then ---make sure that the character exists before continuing
                   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
end
1 Like

For some reason it isn’t playing at all.

Yeah then my code doesn’t work. I know the problem but I don’t know how to fix it. Basically when the player dies, their character dissapears until they respawn. Your script calls on the character so you get an error because after they die there is no character for the script to call on. All of the things that usually would work to check for something like FindFirstChild or WaitForChild don’t work with player.Charcter.

So there’s no way to fix this or alter this? Oof…

Alright I believe this would work try this:

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 player = game.Players.LocalPlayer
                local character  = player.Character or player.CharacterAdded:Wait() --waits until character exists
		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
1 Like

I have to go in a couple minutes, I hope that worked. Good luck with your code!

1 Like

Now it’s only when I die and not reset? So strange, but do you mind looking into it? Thanks! :grinning: