Exhausted allowed execution time in wait lines

I’m creating a system where you can mute songs (parented in the script), the problem is that touching the block that mute the songs would give a timeout in some wait() lines, which i marked in the script:

local IsPlayerInAllowedArea = true

local DefaultVolume = 0.5
local DelayToStartNextSong = 0.5

function StopSongs()
	if IsPlayerInAllowedArea then
		IsPlayerInAllowedArea = false
		local Songs = script:GetChildren()
		for _, Song in pairs(Songs) do
			if Song.IsPlaying then
				Song:Stop()
			end
		end
	end
end

function PlaySongs()
	if not IsPlayerInAllowedArea then
		IsPlayerInAllowedArea = true
		print("Play")
	end
end

function GetRandomSong(PreviousSong)
	local Songs = script:GetChildren()
    local RandomSong
    repeat
        RandomSong = Songs[math.random(#Songs)]
    until
    RandomSong ~= PreviousSong
    return RandomSong
end

local CurrentSong


coroutine.resume(coroutine.create(function()
	while true do
		if IsPlayerInAllowedArea then 
			wait(0.5)
        	local SelectedSong = GetRandomSong(CurrentSong)
        	CurrentSong = SelectedSong
        	print(CurrentSong)
			CurrentSong:Play()
			CurrentSong.Stopped:Wait() --//Timeout here//--
		end
    end
end))
end)

local StopSongsBrick = game.Workspace:FindFirstChild("StopSongs")

local e = false
StopSongsBrick.Touched:Connect(function(Obj)
	if Obj.Parent.Name == game.Players.LocalPlayer.Name and not e then
		e = true
		StopSongs()
		wait(0.5)--//Timeout here//--
		e = false
	end
end)
Explanation about how the script works

IsPlayerInAllowedArea would determine if the songs are going to play, StopSongs() would stop every song that is playing. PlaySongs() would set IsPlayerInAllowedArea true, which would make the next loop select a random song and play it. There’s a block touched function, which would fire the StopSongs() function.

I don’t know how timeouts are occurring, i really need help in this script, if you need more info, feel free to ask me. Thanks for reading.

Try changing “while true do” to “while wait() do”.

I know that’s not considered a good practice by a lot of people, but I’m saying to use it as a temporary test to see if that’s the problem, or as a solution if you don’t want to look for a better way.

EDIT: I think the problem could be with the first timeout, due to the while loop rapidly firing. I’ll keep looking through your code though to see if I’m wrong.