How to control GuiNavigation?

I am trying to adjust the UI Navigation to be enabled at certain times to accommodate for console players. I’ve been looking through a bunch of API, but still couldn’t really find a way to forcefully disable or enable UI Navigation while a UI is currently being navigated on.

Also when playing on Console, some of my Tweens bug the UI’s visual and it doesn’t get corrected until the UI Navigation is turned off or until player presses escape Menu. Any idea why?

2 Likes

You can set GuiService.GuiNavigationEnabled via a client-sided script to true if you want it enabled for the player, and false if you want it disabled for the player, at any given time.

When it’s set to false, the player won’t be able to cycle between different on-screen UIs, but any UI object that had been selected at the time won’t be deselected unless:

  1. The player presses the UI Navigation key again
  2. A client-sided script sets GuiService.SelectedObject to nil

If you want to deselect the currently selected GuiObject at the same time that GuiNavigationEnabled is turned off, you can set SelectedObject to nil.

If you don’t want the player to be able to automatically select any UI objects while GuiNavigationEnabled is turned off, make sure to also turn off GuiService.AutoSelectGuiEnabled, because when it’s false, a client-sided script would need to set the value of SelectedObject to begin UI Navigation.

Example

local GuiService = game:GetService("GuiService")

local function toggleUiNavigationPermissions(newState)
	GuiService.GuiNavigationEnabled = newState
	GuiService.AutoSelectGuiEnabled = newState -- Optional, depending on desired behavior
	
	if newState == false then
		GuiService.SelectedObject = nil
	end
end

toggleUiNavigationPermissions(false)
-- Calling the function with "false" will turn off UI Navigation


toggleUiNavigationPermissions(true)
-- Calling the function with "true" will allow the user to use UI Navigation

There is an issue with what you said. GuiNavigationEnabled and AutoSelectGuiEnabled are read only. Trying to modify them will likely just create errors.

There’s a discrepancy between what it displays on the Roblox Creator Documentation site and what happens when you try to modify it in-game.

Currently, it does show “Read Parallel” on the documentation pages for both GuiNavigationEnabled and AutoSelectGuiEnabled, however, when you test it out in Roblox Studio, it does let you modify those properties during runtime (I had tested the code in my initial reply to this thread before posting it).


To showcase that those properties can be updated during runtime, I’ve created an interactive place file as well as a video that goes through different scenarios when certain properties are enabled / disabled:

Example GUI Navigation Place File.rbxl (65.8 KB)

Example Video


Additionally, for ease of access, here’s the code I wrote for the LocalScript:

local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")

local ScreenGui = script.Parent

local GUIToggle = ScreenGui:WaitForChild("GUIToggle")

local Frame = ScreenGui:WaitForChild("Frame")
local falseButton = Frame:WaitForChild("falseButton")
local trueButton = Frame:WaitForChild("trueButton")
local ClearSelectedObject = Frame:WaitForChild("ClearSelectedObject")
local ResetSelectedObject = Frame:WaitForChild("ResetSelectedObject")
local ToggleGuiNavigationEnabled = Frame:WaitForChild("ToggleGuiNavigationEnabled")
local ToggleAutoSelectGuiEnabled = Frame:WaitForChild("ToggleAutoSelectGuiEnabled")

local GuiNavigationEnabledTextLabel = ScreenGui:WaitForChild("GuiNavigationEnabled")
local AutoSelectGuiEnabledTextLabel = ScreenGui:WaitForChild("AutoSelectGuiEnabled")
local SelectedObjectTextLabel = ScreenGui:WaitForChild("SelectedObject")
local LastKeyPressedTextLabel = ScreenGui:WaitForChild("LastKeyPressed")

---

local function updateTextLabel(property, TextLabelToChange)
	TextLabelToChange.Text = property..": "..tostring(GuiService[property])
	print(TextLabelToChange.Text)
end

GuiService:GetPropertyChangedSignal("GuiNavigationEnabled"):Connect(function()
	updateTextLabel("GuiNavigationEnabled", GuiNavigationEnabledTextLabel)
end)

GuiService:GetPropertyChangedSignal("SelectedObject"):Connect(function()
	updateTextLabel("SelectedObject", SelectedObjectTextLabel)
end)

GuiService:GetPropertyChangedSignal("AutoSelectGuiEnabled"):Connect(function()
	updateTextLabel("AutoSelectGuiEnabled", AutoSelectGuiEnabledTextLabel)
end)

UserInputService.InputBegan:Connect(function(inputObject)
	local keyCode = inputObject.KeyCode
	if not keyCode then return end
	
	LastKeyPressedTextLabel.Text = "Last Key Pressed: "..tostring(keyCode)
end)

---

local function toggleUiNavigationPermissions(newState)
	warn("Before")
	print("GuiNavigationEnabled: "..tostring(GuiService.GuiNavigationEnabled), "AutoSelectGuiEnabled: "..tostring(GuiService.AutoSelectGuiEnabled))
	
	GuiService.GuiNavigationEnabled = newState
	GuiService.AutoSelectGuiEnabled = newState -- Optional, depending on desired behavior
end
toggleUiNavigationPermissions(true) -- Runs right away to update the TextLabels and makes sure the properties are enabled by default


falseButton.Activated:Connect(function()
	toggleUiNavigationPermissions(false)
	-- Calling the function with "false" will turn off UI Navigation
end)

trueButton.Activated:Connect(function()
	toggleUiNavigationPermissions(true)
	-- Calling the function with "true" will allow the user to use UI Navigation
end)

---

ClearSelectedObject.Activated:Connect(function()
	GuiService.SelectedObject = nil
end)

ResetSelectedObject.Activated:Connect(function()
	GuiService.SelectedObject = GUIToggle
end)

ToggleGuiNavigationEnabled.Activated:Connect(function()
	GuiService.GuiNavigationEnabled = not GuiService.GuiNavigationEnabled
end)

ToggleAutoSelectGuiEnabled.Activated:Connect(function()
	GuiService.AutoSelectGuiEnabled = not GuiService.AutoSelectGuiEnabled
end)


GUIToggle.Activated:Connect(function()
	Frame.Visible = not Frame.Visible
end)

Roblox lied to me. Thanks btw. i was about to create my own version of the UI navigation since it was not working for me. You made me save some time

1 Like