String.sub returns � instead of Ñ

Hello, I am currently writing a script which auto generates 3d text using meshes of every letter. This includes letters like ñ, í or è. I loop through the text i want to put in 3d using something like this:

text = 'Héllo'
for i=1,#text do
		game.ServerStorage.Letters['Letter_'..string.sub(text,i,i)]:Clone()
end

However, when the string passes through string.sub, it returns a �(question mark thingy) instead of the letter é, which results in getting Letter_� instead of Letter_é and my script not finding the letter. How do i fix this?

1 Like

Probably because the character isn’t recognised by either the function or by Roblox itself, so it’s replaced with a question mark.

Here’s some more info:

1 Like

Yeah i know, but how do i fix this. Like is there another way to get one letter out of a string of text

Avoid attempting to find non-alphanumeric text.

I cant, because I want this to be dynamic with every letter. That way i can translate 3d texts for other languages

Because "é" is counted as two characters in string encoded in UTF-8, C9 A9.

In that case, you can use utf8.sub in lieu of string.sub

Here’s an implementation of utf8.sub from Lua unicode, using string.sub() with two-byted chars - Stack Overflow

function utf8.sub(s,i,j)
    i=utf8.offset(s,i)
    j=utf8.offset(s,j+1)-1
    return string.sub(s,i,j)
end

Though you can’t modify the content of the utf8 table, you might want to rename the function.

2 Likes

You could use gsub(), and attempt to replace the string with a recognisable string like “ewithaccent” and then perform the sub() function on that new string looking for “ewithaccent” instead.

1 Like

But it doesn’t have utf8.sub though.

I recognise that, that’s why I deleted the post immediately - don’t know if that showed up though. That’s my bad because I didn’t check which methods were ported over.

1 Like

Thanks, I got it fixed!

2 Likes