Unable to display special characters in string using gsub

Hello! I am currently working on a game where I need text to be set through a script, with a special character. I am using the code below to add text to a table of tables {{{char: string, textColor: Color3?, bgColor: Color3?}}}

although when using special characters such as “█” it gets converted into “�”. I tried messing around with utf8 and nothing really seemed to work. Any help is appreciated, here is the code I used below:

function module.AddText(text: string, cur_pos: Vector2)
    if not chars[cur_pos.Y + 1] then
        chars[cur_pos.Y + 1] = {}
    end

    local row = chars[cur_pos.Y + 1]

    for i = #row + 1, cur_pos.X do
        row[i] = { char = "" }
    end

    local i = 0
    for c in text:gmatch(".") do
        i += 1
        row[cur_pos.X + i] = {
            char = c,
            textColor = curr_color,
            bgColor = curr_bg_color
        }
    end

    module.UpdateTextLabels()
end

module.AddText("RoTerminal [Version 2.0.0]", Vector2.new(0, 0))
module.AddText("(c) Indium. All rights reserved.", Vector2.new(0, 1))

module.SetTextColor(Color3.fromRGB(143, 143, 143))
module.SetBackgroundColor(Color3.fromRGB(255, 135, 135))
module.AddText("█     █", Vector2.new(0, 2))

module.AddText("█     █", Vector2.new(0, 4))
module.AddText(" █████ ", Vector2.new(0, 5))
-- should draw smiley face, instead draws a mess of weird characters

Please note that this isn’t an issue with the font, I can manually add to the textbox, but when I use the .AddText method it shows the question mark.

You need this library for that

and utf8 | Documentation - Roblox Creator Hub more specifically

1 Like

could you provide an example on how to use graphemes for this? I tried something with it earlier & couldn’t get it working, but maybe I’m understanding it wrong.

I figured it out, I was able to do

for first, last in utf8.graphemes(text) do
    local char =  string.sub(text, first, last)
end
local chars = {}
local curr_color = Color3.new(1, 1, 1)
local curr_bg_color = Color3.new(0, 0, 0)

function AddText(text, cur_pos)
	if not chars[cur_pos.Y + 1] then
		chars[cur_pos.Y + 1] = {}
	end
	local row = chars[cur_pos.Y + 1]
	for i = 1, cur_pos.X do
		if not row[i] then
			row[i] = { char = " " }
		end
	end
	local i = 0
	for _, codepoint in utf8.codes(text) do
		i += 1
		row[cur_pos.X + i] = {
			char = utf8.char(codepoint),
			textColor = curr_color,
			bgColor = curr_bg_color
		}
	end
end

AddText("RoTerminal [Version 2.0.0]", Vector2.new(0, 0))
AddText("(c) Indium. All rights reserved.", Vector2.new(0, 1))
AddText("█     █", Vector2.new(0, 2))
AddText(" █   █", Vector2.new(0, 3)) -- this line has to be different than the one before it.
AddText("  ███ ", Vector2.new(0, 4))

local maxLength = 0
for _, row in pairs(chars) do
	if #row > maxLength then maxLength = #row end
end

for y = 1, 5 do
	local row = chars[y] or {}
	local line = ""
	for x = 1, maxLength do
		line ..= (row[x] and row[x].char or " ")
	end
	print(line)
end

I see my testing once again took too long… :frowning:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.