Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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 answer

Log in to vote
1
Answered by 7 years ago

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)
0
Thank you! NovaMagic 73 — 7y
0
there is also a method called TextBox.IsFocused (or TextBox:IsFocused() ), returns true or false. RubenKan 3615 — 7y
Ad

Answer this question