How to check how many letters are in a TextBox?
The Text
property of the TextBox holds a string, so just get the length of that.
#TextBox.Text
string.len and #text should check how many chars in a text.
For example, if you prefer string.len, you could do:
print(string.len("Hello")) --> 5
I think using string.len
is overkill, considering there is the #
operator for getting the length of strings (and tables!) already.
Calling functions directly from the string library has a pending optimisation which runs quicker than method calls. No, the length operator isn’t a method call, the only important bit is that “optimisation”.
I don’t know if string.len would be faster post-optimisation despite the speed being fairly negligible, but using string.len isn’t overkill. It’s pretty clear, as is the length operator, to what you’re doing. If you’re a real nutcase for consistency in what you use, then that as well. Don’t forget about utf8.len if you’re working with characters more than one byte, hence consistency.
Both options are no less viable than the other unless of course that micro amount of speed and how your code looks is that important to you. Depending on your use case, functional value as well.
Try this:
local TextBox = script.Parent.TextBox
local Text = TextBox.Text
local Letters = string.len(Text)
print(Letters)