I've been wondering how to set a maximum character limit. While I'm on this question, is there a way to tell the amount of letters/number in a value? I know I could use :gmatch and compare it to all letters, but that would take up a lot of time. Any shorter way?
string.len should help point you in the right direction.
The function string.len(s) returns the amount of characters that are in a string, the parameter s being any string.
Using this knowledge, the fragment below should give you an idea of what you would be looking for:
string = "This is your string" --This has 19 characters in it. stringLimit = 10 --This could be the limit for the amount of characters in a string. if string.len(string) > stringLimit then print("There are too many characters in the string!") --Use ways such as string.sub() to change the string into what you want. end
NOTE: The fragment above is not its own script, you would have to find something to trigger this conditional statement.
If I helped you, make sure to hit that Accept Answer button below my character! :D
A good way to measure the length of a string is using the # operator
So, you want to create a 'character limit' for a stringvalue? To do this you can have an 'oldValue' variable at the top of the script, then using the Changed event you can check if the new value exceeds the limit. If it does then set the value to the 'oldValue' variable, if it doesn't then update the 'oldValue' variable.
local limit = 100 --The limit local oldValue = workspace.StringValue.Value workspace.StringValue.Changed:connect(function(change) if #change > limit then workspace.StringValue.Value = oldValue else oldValue = change end end)