How do I make a a textbox only allow 1 letter?

So in my game I have these keybinds and I want the player to be able to change them, so far I have it so that they can only type in letters and they can’t type in numbers or spaces in the textbox.

The issue is that they can type in more than one letter, which will break the script.

This is some of the script:

local KeyBindBox = script.Parent.SettingsFrame.ScrollingFrame.ResetKeyBox
local ResetKeyBind = "R"

function onlyLetters(text)--This is the function that makes it so that they can only type letters
	return text:gsub("[%A%s]+", "")
end

KeyBindBox:GetPropertyChangedSignal("Text"):Connect(function()
	KeyBindBox.Text = onlyLetters(KeyBindBox.Text)
	KeyBindBox.Text = string.upper(KeyBindBox.Text)
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode[ResetKeyBind] then
			game.ReplicatedStorage.PlayerReset:FireServer()
		end
	end
end)

KeyBindBox.Focused:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProcessedEvent)
		if gameProcessedEvent then
			if input.KeyCode == Enum.KeyCode.Return then
				ResetKeyBind = string.upper(KeyBindBox.Text)
			end
		end
	end)
end)

You can use string.sub, to manipulate your string and thus get only the first digit,
Put this code in KeyBindBox:GetPropertyChangedSignal

if string.sub(KeyBindBox.Text,1,1) then
	KeyBindBox.Text = string.sub(KeyBindBox.Text,1,1)
end
2 Likes
  text = text:match("[^%c%d]+"):match("%a")

To run everytime the text changes, what this does is that it matches anything that is not a control character or digit, then matches just one lowercase or uppercase letter.