How to add a limit for how many characters the player can write in a TextBox?

title says it all

You can use textbox:GetPropertyChangedSignal("Text") of the textbox and then set the textboxes text to your limit number : textbox.text = string.sub(textBox.Text, 1, maxCharacters)

3 Likes

You can set the MaxVisibleGraphemes property to your character limit number you want.

2 Likes

True

And if you don’t need to monitor text changes for any other purpose you can connect and disconnect the event on TextBox Focus and FocusLost for performance and run the code in the function one last time after FocusLost if you wanna be extra safe

2 Likes

That´s a very good addition! Use this definitely.

1 Like

A better idea is to merge Modern_Developer2201’s idea to set the MaxVisibleGraphemes and also implementing your idea for more optimization.

1 Like

Oh yeah, that would be the best implementation as it achieves the result we need with no text change connection or focus connection necessary

1 Like

I’d personally say either this or that post is the solution Sheriff, as the part of the code that actually limits the characters is still @Andolino13’s textbox.text = string.sub(textBox.Text, 1, maxCharacters)

The MaxVisibleGraphemes is just the visual aspect that helps the player know when they’ve reached the limit by hiding the excess characters from them

2 Likes

Use MaxVisibleGraphemes if you want a simple, clean solution and don’t need extra behavior beyond visually limiting input.
Use GetPropertyChangedSignal if you need more control such as filtering characters, performing other logic of yours or ensuring that the actual text length cannot be changed even with script manipulation.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.