Special characters seem to be worth more than 1 character?

Hello!

I’m working on making a playlist manager and I’ve run into an issue where if you enter non-alphanumeric/non-whitespace characters, they appear to be worth more than 1 character (and just for reference, I’m looking to give players the option to write up to 20 characters in a textbox). And at the end, it has a box with an X in it.

Video:


Script:

		input:GetPropertyChangedSignal('Text'):Connect(function()
			local newText = input.Text:sub(1,20) -- this limits it to 20 characters for input, so a player *should* be able to write up to 20 characters, regardless of whether they're alphanumeric or not
			input.Text = newText
			if lastText ~= newText then
				local currentTick = tick()
				lastTick = currentTick
				previewLabel.Text = formatString:format(input.Text:gsub('[^%s]', '_'))
				local filtered = formatString:format(filterFunction:InvokeServer(input.Text))
				if lastTick == currentTick then
					previewLabel.Text = filtered
				end
			end
			lastText = newText
		end)

That’s because in UTF-8 encoding, special characters often require more than 1 byte to be represented.

Figured it out, you have to use utf8.len, I don’t think there’s any equivalent of string.sub but I was able to get this working for my use case:

			local newText = input.Text
			local length = utf8.len(newText)
			if length > 20 then
				newText = lastValidLength
			else
				lastValidLength = newText
			end
			input.Text = newText
1 Like