Textbox Scripting Question

In my game there is a textbox you put roman numerals into. Certain roman numeral symbols can’t be repeated more than a certain number of times in a row, so this script is meant to set a limit. It succeeds in setting a limit to how many times a symbol can repeat throughout the whole string, but I need it to be for consecutive symbols only. For example, “MMMCMXCIX” should be allowed (more than 3 m’s but no more than 3 m’s in a row) but “MMMM” shouldn’t be allowed (more than 3 m’s in a row). The script below wouldn’t accept that first string even though it’s a valid roman numeral because there is more than 3 m’s. What do I do to make the limit be for symbols in succession only?

IS = 0
VS = 0
XS = 0
LS = 0
CS = 0
DS = 0
MS = 0

Input:GetPropertyChangedSignal("Text"):Connect(function()
    symbols = {} -- Resets the list of symbols so they don't stack every time the text changes
    IS = 0 -- Resets the count for each character so they don't stack every time the text changes
    VS = 0
    XS = 0
    LS = 0
    CS = 0
    DS = 0  
    MS = 0
    for v in Input.Text:gmatch(".") do -- Put every character of the string into a list
        table.insert(symbols, v)
    end
    for i,v in pairs(symbols) do -- Counts how many times each symbol appears in the list
        if string.upper(v) == "I" then
            IS = IS + 1
        end
        if string.upper(v) == "V" then
            VS = VS + 1
        end
        if string.upper(v) == "X" then
            XS = XS + 1
        end
        if string.upper(v) == "L" then
            LS = LS + 1
        end
        if string.upper(v) == "C" then
            CS = CS + 1
        end
        if string.upper(v) == "D" then
            DS = DS + 1
        end
        if string.upper(v) == "M" then
            MS = MS + 1
        end
    end
    if IS > 3 then
        local substring = Input.Text:sub(1, #Input.Text - 1) -- Removes last character if limit is exceeded
        Input.Text = substring 
    end
    if VS > 1 then
        local substring = Input.Text:sub(1, #Input.Text - 1)
        Input.Text = substring 
    end
    if XS > 3 then
        local substring = Input.Text:sub(1, #Input.Text - 1)
        Input.Text = substring 
    end
    if LS > 1 then
        local substring = Input.Text:sub(1, #Input.Text - 1)
        Input.Text = substring 
    end
    if CS > 3 then
        local substring = Input.Text:sub(1, #Input.Text - 1)
        Input.Text = substring 
    end
    if DS > 1 then
        local substring = Input.Text:sub(1, #Input.Text - 1)
        Input.Text = substring 
    end
    if MS > 3 then
        local substring = Input.Text:sub(1, #Input.Text - 1)
        Input.Text = substring 
    end
end)
1 Like

You could try using this right here instead. What it does is detect if any alphabetical character appears more than 3 times, which fits your purpose.

1 Like

You would need to check if a different character than (for example) M, has been added, and then reset tthe count

local TextBox = script.Parent

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	for Character in TextBox.Text:gmatch(".") do
		local Match = TextBox.Text:match(Character:rep(4))
		if Match then
			TextBox.Text = TextBox.Text:gsub(Match, Character:rep(3))
		end
	end
end)

This will work for any character not just roman numerals.

https://gyazo.com/dc01c53d38fe00e9f0c1835c035752e8

1 Like

Works great, thanks. One problem though: I replaced the period with a letter since different letters need different limits. But when I do that it can only detect a capital or a lowercase version of that letter depending on what I put in the parameter. How do I make it detect the letter regardless of capitalization?

local TextBox = script.Parent

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:upper()
	for Character in TextBox.Text:gmatch(".") do
		local Match = TextBox.Text:match(Character:rep(4))
		if Match then
			TextBox.Text = TextBox.Text:gsub(Match, Character:rep(3))
		end
	end
end)

If it’s roman numerals would it not be feasible to just enforce capitalisation?

1 Like