Okay so, I’ve been trying to create a typewriter-like dialog for my game, but I’ve been having issues while integrating translations.
My system is supposed to make the dialog pause briefly whenever it finds a letter with a specific punctuation (such as “,” or “.”) however as it’s shown in this video, the dialog pauses later than it’s supposed to, which makes it seem odd.
And this is how my code currently looks:
local Punctuations = {[","] = 0.4, ["."] = 0.4}
-- Function to typewrite text --
function typewrite(label, text)
-- Setup text and typewriter effect
label.MaxVisibleGraphemes = 0
label.Text = text
repeat
label.MaxVisibleGraphemes += 1
cloneSound(textSound)
-- Adds a brief pause for punctuations
local timeDelay = label.LocalizedText:sub(label.MaxVisibleGraphemes, label.MaxVisibleGraphemes):match("%p") and Punctuations[label.LocalizedText:sub(label.MaxVisibleGraphemes, label.MaxVisibleGraphemes):match("%p")] or 0.03
task.wait(timeDelay)
until not label or label.MaxVisibleGraphemes >= string.len(label.LocalizedText)
end
typewrite(textLabel, "This place looks messed up... Is this really where they live, mom?")
I’m not very great with string and I tried seeking help from other posts but couldn’t find much useful information for my problem, so feedback would be appreciated!
Does it have something to do with the translating? I tried it and it worked perfectly fine, but I see you have the text in Spanish, where some words are longer than other resulting to it pausing at wrong place. Is it maybe possible to translate the text before starting to print it?
Sorry it took so long, but I found the problem. It has to do with the fact that some letters, “á” for example, counts as two characters, messing up the way the text is displayed.
local Punctuations = {[","] = .4, ["."] = .4}
local textSound = script.Parent.text
local textLabel = script.Parent.TextLabel
-- Function to typewrite text --
function typewrite(label, text)
-- Setup text and typewriter effect
label.MaxVisibleGraphemes = 0
label.Text = text
for i, codepoint in utf8.codes(label.LocalizedText) do
local letter = utf8.char(codepoint)
label.MaxVisibleGraphemes += 1
textSound:Play()
local timeDelay = Punctuations[letter] or .03
task.wait(timeDelay)
end
end
typewrite(textLabel, "this woérks. évén with all. thése wéáird cháracters! ")
This script uses utf8 library which makes the for loop skip over these “double-characters”, fixing the issue. It also makes it easier to get the letter (and the timedelay), not having to sub, match and all of that.