Reverse a Utf8 String

At the time of this post, roblox has a bug with displaying utf8 characters for languages that are written Right to Left, which would display backwards if written in a UI.

I wrote a little function which takes in a utf8 string and reverses it so that you can put it in your UI and having the text appear correctly for your players.

function ReverseUtf8Rtl(word)
	local utf8Words = {}
	local utf8Reversed = {}
	local utf8CurrentWord = ""
	for first, last in utf8.graphemes(word) do
		local Utf8char = word:sub(first,last)
		if string.find(Utf8char, "%s+") == nil then
			utf8CurrentWord = utf8CurrentWord .. Utf8char
			table.insert(utf8Words, utf8CurrentWord)
			utf8CurrentWord = ""
	table.insert(utf8Words, utf8CurrentWord)
	for i = #utf8Words, 1, -1 do
		table.insert(utf8Reversed, utf8Words[i])
	return table.concat(utf8Reversed, " ")

this is actually my first time i’ve ever used the utf8 stuff roblox added, i never knew it existed till i started messing around trying to reverse my strings! If you know of a better way to do this, feel free to share!

Here is a string you can play around with:
التفاحة قد صقطت من الشجرة

Which means “The apple from the tree”