I want to use the typewriter module from this tutorial.
(please don’t say to tell me to do it this way
for i = 1, #text do
guiObject.Text = string.sub(guiObject.Text, 1, i)
wait()
end
This is so I can have Localization support and utf8 support. That’s why I want to use this module instead of the traditional way)
The main issue is the fact that whenever there is a lag spike, the letters in the text scramble (is this some type of undocumented behavior? could it be a bug?).
I searched on the roblox api on utf8.graphemes
, however, it only provides vague information for the function. Therefore, I am was unable to find a solution to my problem.
If anybody would like the code, here it is:
local SOURCE_LOCALE = "en"
local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local translator
pcall(function()
translator = LocalizationService:GetTranslatorForPlayerAsync(player)
end)
if not translator then
pcall(function()
translator = LocalizationService:GetTranslatorForLocaleAsync(SOURCE_LOCALE)
end)
end
local TypeWriter = {}
local defaultConfigurations = {
delayTime = 0.2,
extraDelayOnSpace = true
}
function TypeWriter.configure(configurations)
for key, value in pairs(defaultConfigurations) do
local newValue = configurations[key]
if newValue ~= nil then
defaultConfigurations[key] = newValue
else
warn(key .. " is not a valid configuration for TypeWriter module")
end
end
end
function TypeWriter.typeWrite(guiObject, text)
guiObject.AutoLocalize = false
guiObject.Text = ""
local displayText = text
if translator then
displayText = translator:Translate(guiObject, text)
end
for first, last in utf8.graphemes(displayText) do
local grapheme = string.sub(displayText, first, last)
guiObject.Text = guiObject.Text .. grapheme
if defaultConfigurations.extraDelayOnSpace and grapheme == " " then
wait(defaultConfigurations.delayTime)
end
wait(defaultConfigurations.delayTime)
end
end
return TypeWriter
After some debugging, I can make sure that:
- The text scrambling is NOT caused by translating the text (see
displayText = translator:Translate(guiObject, text)
in an if statement) - The scrambling occurs AFTER the translating (to be more specific, the iteration)
- NO errors are being produced.
- All GuiObjects and Text arguments passed to
Typewriter.typeWrite
is NOTnil
- There is NO difference in results between
string.sub
from the string library andtext:sub
method