Limiting maximum characters in a TextBox

I see there is no default control in Studio to limit the maximum characters input in a TextBox.
So I decided to limit using UserInputService.InputBegan:

UserInputService.InputBegan:Connect(function(Input,GPE)
	if string.len(myTextBox.Text) > 10 then
		myTextBox.Text = string.sub(myTextBox.Text, 1, 10)
	end
end

However, I noticed that string.len(myTextBox.Text) is not being updated inside UserInputService.InputBegan, but only after the rendering…

I’d like to know, what’s the best way to limit the number of characters typed in a TextBox?
That is, when the limit is exceeded, the TextBox stops receiving new typing.

7 Likes

I suggest resizing the textbox, turning the background invisble and adding a frame which takes over the old background of the textbox, this is an easy solution and temporary solution so it won’t work for when the person uses only smaller letters like I.

You can hook a Changed event to the textbox, although not the best solution, it probably works fine:

TextBox:GetPropertyChangedSignal("Text"):Connect(function() -- Fires when property, "Text" changes
    if string.len(TextBox.Text) > 10 then
        TextBox.Text = string.sub(TextBox.Text, 1, 10)
    end
end)
3 Likes

literally just

myTextBox.Changed:Connect(function()
    myTextBox.Text = myTextBox.Text:sub(1,10)
end)

that’s how i always do it, and it hasn’t gave me any issues.

32 Likes

the code you provided should do what you want it do to, i tested it in studio and everytime you hit a key, it checks if the amount of characters in the text box is greater than 10, if so then it subs 1 to 10.

things to note, it only updates when a key is pressed, so if you hold down a key, it will put in more than 11.

p.s. the code above that camper and crundee provided is probably better than user input service, and you could also you use the textbox.focused event and unfocused event.

2 Likes

I tested but .Changed is not being fired while typing.
I’d like to block all excessive inputs while typing.

Probably is since we’re trying to limit the amount of characters here and we know it would be suitable to run checks only when the text actually changes, not whenever a user performs any kind of input on that box (even when you filter user input to only check for keypresses you’re just missing out on utilizing a function to make your life easier).

You could do something like:

--//

local box = box;
local sub, len = string.sub, string.len;

local function onTextChanged()
    local text = box.Text
    box.Text = len(text) > 10 and sub(text, 1, 10) or box.Text --// or just use an if statement to not set the Text property to anything if it's not more than 10 characters
end

box:GetPropertyChangedSignal("Text"):Connect(onTextChanged)

Better to use GPCS just to listen for changes in text rather than .Changed to listen for a change in any property of the object.

There is also no use of using events like RunService.RenderStepped since a GPCS(Text) listener would work nearly as good due to how frequently events fire on their own.

because events are efficient on their own compared to checking every frame, they fire every few frames anyways when they need to be triggerred.

2 Likes

What you could do is:

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(step)
	if string.len(TextBox.Text) > 10 then
		TextBox.Text = string.sub(TextBox.Text, 1, 10)
	end
end)

This would run every frame or so in a local script and probaly work; you might see a letter for only 1 frame, but that’s it.

instead use

1 Like

think it’s more to do with your script position than it is anything else, .Changed works just fine for me, and TextBoxes do not otherwise change their other properties enough that needing anything other than .Changed is warranted

1 Like

well I mean, you could… I wouldn’t check that every frame though.

if you use :GetPropertyChangedSignal or .Changed like they are recommending, that is the best in every way, you don’t get the small bug that mentioned when you hold the key, and you are only checking when the player types.

@VegetationBush @Camper0008
Thanks, both suggestions are working. :+1:

1 Like

Would it not be more efficient to do

myTextBox:GetPropertyChangedSignal('Text'):Connect(Function()