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

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

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

✓ Accepted answer

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)

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