"Setting GuiService.SelectedObject to invalid GuiObject" when object is a valid GuiObject

Hello developers,

I am currently reworking my menu code and how it works, but I have ran into a problem. When you change pages on a gamepad, it should automatically select the button with the smallest SelectionOrder, but it doesn’t. Here’s the code that will select it:

function Package.SelectLowestOrder(page: Frame)
	for index, value in page:GetChildren() do
		if value:IsA("GuiObject") and value.SelectionOrder == 1 then
			GuiService.SelectedObject = value
		end
	end
end

Code that changes pages:

if values[2] or values[4] then
		PageToAdvanceTo.Visible = true
		PageToAdvanceTo:SetAttribute("Enabled", true)
		PageToAdvanceTo.Position = PreTweenUDims.In

		PageToAdvanceTo:TweenPosition(TweenUDims.In, Enum.EasingDirection.In, Enum.EasingStyle.Quad, TweenTime)
		
		if selectEnabled and UserInputService.GamepadEnabled then
			Package.SelectLowestOrder(PageToAdvanceTo)
		end
		
		task.wait(TweenTime)
	end

It’s really weird, it works randomly.

Help please? I need this issue fixed ASAP.

I tried setting SelectedObject to a frame in PlayerGui, it looks like it’s working so I’m guessing you can probably just ignore the warning message.

local guiService = game:GetService('GuiService')

guiService.SelectedObject = game.Players.LocalPlayer.PlayerGui:WaitForChild('ScreenGui').Frame

Yeah, but when I see this message it no longer selects it.

I don’t know why that is happening because I can’t seem to recreate it for some reason. It’s selecting the GUI object for me. Have you tried restarting Roblox Studio?

local GuiService = game:GetService('GuiService')

selectEnabled = true
TweenTime = 1

function SelectLowestOrder(page: Frame)
	for index, value in page:GetChildren() do
		if value:IsA("GuiObject") and value.SelectionOrder == 1 then
			GuiService.SelectedObject = value
		end
	end
end

if true then -- values[2] and values[4]
	local PageToAdvanceTo = game.Players.LocalPlayer.PlayerGui:WaitForChild('ScreenGui').Frame
	PageToAdvanceTo.Visible = true
	PageToAdvanceTo:SetAttribute("Enabled", true)
	PageToAdvanceTo.Position = UDim2.fromScale(0, 0) -- PreTweenUDims.In
	
	PageToAdvanceTo:TweenPosition(UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.In, Enum.EasingStyle.Quad, TweenTime)
	
	if selectEnabled and true then -- UserInputService.GamepadEnabled
		SelectLowestOrder(PageToAdvanceTo)
	end
	
	task.wait(TweenTime)
end

Edit: I also tried putting the function into a ModuleScript but it still selects it for me.

My assumption is that the GuiObject also needs to be Active, and Selectable for it to be valid. If it’s just a frame, it might not be valid either? I tried to make console support once, but it was almost the last thing I tried in this life. It sucked.

2 Likes