------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


function Controller.UpdateSelections()
	local iteration = 0
	---
	local highestZ = 0
	for _, screenGui in ipairs(playerGui:GetChildren()) do
		if screenGui.ClassName == "ScreenGui" then
			if screenGui.Enabled == true and screenGui.DisplayOrder > highestZ and screenGui.Name ~= "Chat" and screenGui.Name ~= "Info Overlay" then
				local buttonVisible = false
				local function scanElement (p)
					if buttonVisible == false then
						for _, v in ipairs(p:GetChildren()) do
							iteration = iteration + 1
							if iteration%300 == 0 then
								_L.Services.RunService.Stepped:wait()
							end
							if v:IsA("GuiObject") then
								if v.Visible == true then
									if v.ClassName == "ImageButton" or v.ClassName == "TextButton" or v.ClassName == "TextBox" then
										buttonVisible = true
										return
									end
									scanElement(v)
								end
							end
						end
					end
				end
				scanElement(screenGui)
				if buttonVisible then
					highestZ = screenGui.DisplayOrder
				end
			end
		end
	end
	---
	local function scan(parent)
		for _, child in ipairs(parent:GetChildren()) do
			if child then
				if child.ClassName == "ImageButton" or child.ClassName == "TextButton" or child.ClassName == "TextBox" then
					if not blacklistSet then
						if child.Selectable == false then
							selectionBlacklist[child] = true
						end
					end
					
					if selectionBlacklist[child] == nil then
						local selectable = true
						if (child.ClassName == "ImageButton" and child.ImageTransparency == 1) or child.Visible == false then
							--- Blunt test
							selectable = false
						else
							--- Parent test
							local function scanParent(p1)
								iteration = iteration + 1
								if iteration%300 == 0 then
									_L.Services.RunService.Stepped:wait()
								end
								local p2 = p1.Parent
								pcall(function()  if p2.Visible == false then  selectable = false  end  end)
								pcall(function()  if p2.ClassName == "ScreenGui" and (p2.Enabled == false or p2.DisplayOrder < highestZ) then  selectable = false  end  end)
								pcall(function()  if p2.ClassName == "SurfaceGui" or p2.ClassName == "BillboardGui" then  selectable = false  end  end)
								if selectable == true then
									if p2 ~= nil and p2 ~= playerGui then
										scanParent(p2)
									end
								end
							end
							scanParent(child)
						end
						child.Selectable = child:FindFirstAncestor("Tutorial") ~= nil and true or selectable
					end
				else
					pcall(function()  child.Selectable = false  end)
				end
				scan(child)
			end
		end
	end
	---
	scan(playerGui)
	blacklistSet = true
end