Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to tell if a player is focused on a textbox or not?

When someone types n in chat it fires this script

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.N then

		local statsGui = script.Parent.StatsBorder
		local invGui = script.Parent.Parent.InventoryGuiBG.InventoryBorder

		if statsGui.Visible == true then
			statsGui.Visible = false
		elseif statsGui.Visible == false then
			invGui.Visible = false
			statsGui.Visible = true
		end
			
	end
end)

How can I tell if a player is focused on a textbox or not?

1 comment

1 answer

✓ Accepted answer

The UserInputService has a function called GetFocusedTextBox that returns the current focused TextBox. If no TextBox is focused, it returns nil. So you can use it in your script to not allow firing the function if a TextBox is being focused.

So your code should look like this:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:connect(function(input)
	if not UserInputService:GetFocusedTextBox() then
	    if input.KeyCode == Enum.KeyCode.N then
	
	        local statsGui = script.Parent.StatsBorder
	        local invGui = script.Parent.Parent.InventoryGuiBG.InventoryBorder
	
	        if statsGui.Visible == true then
	            statsGui.Visible = false
		elseif statsGui.Visible == false then
	        	invGui.Visible = false
	           	statsGui.Visible = true
	        end
   	    end
	end
end)

2 comments

0

Thank you!

0

there is also a method called TextBox.IsFocused (or TextBox:IsFocused() ), returns true or false.

This discussion is preserved as it was. New answers, comments, and votes are closed.