Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to set a maximum limit on a StringValue?

Asked by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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?

0
strings can only hold up to 200,000 characters If you want, you could use string.sub? That could work in some cases but not all. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by
Discern 1007 Moderation Voter
9 years ago

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

0
What do you mean,"find something"? I understand it's conditional, but from my point of view, the script looks complete. If not, please explain more. Shawnyg 4330 — 9y
0
By "find something to trigger this conditional statement" I mean like a function or something among those lines. If you always want a limit on the StringValue, then you could use the .Changed event to run this fragment every time the value of the StringValue is changed. It's not complete as in you have to script the limiting of the value instead of just printing a string, and as I said before, you Discern 1007 — 9y
0
...and as I said before, you would need to add a function so it continues to limit; not when you initially join. Discern 1007 — 9y
Ad
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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)

Answer this question