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

Why doesn't the textbox becomes focused when key pressed?

Asked by 5 years ago
Edited 5 years ago

In my custom Chat GUI the Text Box in which the user will type whatever he/she wants to chat. I want to focus the text box when a key is pressed, but it says that Focused is not a valid member of TextBox. Please tell me how I can fix this.

Here is the script:

local userInput = game:GetService("UserInputService")

local function inputBegan(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode
        if key == Enum.KeyCode.Slash or key == Enum.KeyCode.BackSlash then
            script.Parent.Focused = true
        end
    end
end

userInput.InputBegan:Connect(inputBegan)

Thanks!

1 answer

Log in to vote
2
Answered by 5 years ago

its because Focused isnt a property of TextBox which you should probably know because the error says it. so instead just use :CaptureFocus() which forces the client to focus on the textbox.

local userInput = game:GetService("UserInputService")

local function inputBegan(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode

        if key == Enum.KeyCode.Slash or key == Enum.KeyCode.BackSlash then
            script.Parent:CaptureFocus()
        end
    end
end

userInput.InputBegan:Connect(inputBegan)
Ad

Answer this question