hi! i have a typewriter script as a part of my dialog system, partially my code and partially some i found on the forums. it works how you’d expect, with the added feature of looking for “” in dialog to let me put pauses in dialog.
problem is, i’ve discovered that if i put multiple of those in one set of dialog, it only pauses a few characters AFTER it should. for example, the dialog string here is:
"...<!--2-->okay, seriously,<!--2--> who IS that guy?"
but renders like this, with the second pause far after the comma:
this is the typewriter section of my code:
local index = 0
local label = DialogGui.Frame.BorderFrame.InnerFrame.DialogLabel
-- goes through every letter for a typewriter effect
-- if it sees <!--[number]--> it'll pause for the given length of time
label.Text = text
local format_start = "<!--"
local format_end = "-->"
local waitTime = 0
for first, last in utf8.graphemes(label.Text) do
local grapheme = label.Text:sub(first, last)
-- If the next characters after the grapheme are <!--
if label.Text:sub(last+1, last+string.len(format_start)) == format_start then
-- Find a closing -->
local startIndex, endIndex = string.find(label.Text, format_end, last+string.len(format_start))
if startIndex then
-- Get the text between the two
local numberString = label.Text:sub(last+string.len(format_start)+1, startIndex-1)
-- If the text is a number update the `waitTime`
local number = tonumber(numberString)
if number then
waitTime = number
end
end
end
index += 1
if grapheme ~= " " then
label.MaxVisibleGraphemes = index
task.wait(waitTime)
waitTime = 0
end
end
end)
i haven’t been able to figure out why it messed up like this for the life of me. i’ve tried removing the “is the grapheme a space” check, moving the index += 1 to the top of the function, etc, but none of that worked. if anyone can think of a potential solution, i’d really appreciate it
Your index is offset by the length of the the tag. You’d need some way to make your index skip to the end of the tag when a tag is encountered.
I might recommend some preprocessing step to chunk it up in sections of wait time by list of graphemes, or even at the individual character level.
Here’s an implementation that creates a delay for each grapheme and stores it in a list, then just iterates over that list.
local label = script.Parent
local originalText = label.Text
local DEFAULT_DELAY = 0.03 -- Set to nil for no delay, 0 for smallest delay
-- Parse tags and produce per-character delays
local function parseCharacterDelays(text: string)
local chars = {}
local pos = 1
local currentDelay = 0
local function addGraphemes(str)
for first, last in utf8.graphemes(str) do
local grapheme = str:sub(first, last)
local isWhitespace = grapheme:match("%s")
local thisDelay = if isWhitespace then nil else currentDelay
-- Reset the current delay if it's not whitespace
currentDelay = if isWhitespace then currentDelay else DEFAULT_DELAY
table.insert(chars, {
grapheme = str:sub(first, last),
delaySeconds = thisDelay,
})
end
end
while pos <= #text do
local tagStart, tagEnd, number = text:find("<!%-%-(%d*%.?%d+)%-%->", pos)
if not tagStart then
-- No more tags, add the rest of the text
addGraphemes(text:sub(pos))
break
end
-- Add graphemes up to this tag
addGraphemes(text:sub(pos, tagStart - 1))
currentDelay = tonumber(number) or 0
-- Move the iterator past this tag's end
pos = tagEnd + 1
end
return chars
end
-- Build per-character data
local chars = parseCharacterDelays(originalText)
-- Run animation (assumes label.RichText is enabled to hide the tags)
label.MaxVisibleGraphemes = 0
label.Text = originalText
for index, charData in chars do
if charData.delaySeconds ~= nil then
task.wait(charData.delaySeconds)
end
label.MaxVisibleGraphemes = index - 1
end
label.MaxVisibleGraphemes = -1
…there may be simpler ways than this, but hey it works
"...<!--2-->okay, seriously,<!--2--> who👨🤦 IS that guy?✌️"
this works typewriting wise just fine, but doesn’t seem to pause at all. i have a feeling it’s a problem with the text:find string matching bit, but i have NO clue how to decipher that to figure out a fix. can you maybe explain that specific part a little bit?
especially as i can’t figure out how it’s meant to be returning three values, as string:find() seems to be different than string.find() and i can’t find any information about the former
(also, ignore the last post, deleted it because it was user error)
EDIT: never mind! i did research on patterns, figured out i accidentally put a space in there, and now it seems to work fine. not with stuff like 0.25 vs 0.2, but i just added a + after the second digit and it works completely fine now. sorry for all the trouble, and thank you so much for the fix!!!
For anyone else who is wondering what this gobbeldygook is if they come across this solution in the future:
This is a Lua string pattern, like connie mentioned.
What the pattern represents is finding a specific pattern within a string and capturing part of that pattern and returning it.
The beginning and end of the pattern is just this:
<!%-%- finds <!--
%-%-> finds -->
The reason it has the extra % symbols is to ‘escape’ the minus signs, since they are special characters that would mean a “non-greedy repeated sequence” within a pattern. By prefixing a special character with %, that converts it to a literal character.
The middle section is surrounded by ( and ) which creates a capture group. In the case of local start, end, matched = string.find[...], whatever is in the capture group will be returned and stored in matched
The pattern in the capture group is made up of 3 parts
%d*
%d represents a digit, e.g. 0 through 9. The * after it means “0 or more repeating”.
In this case, it’s meant to represent the possible digits before a decimal point in a number.
%.?
%. is an escaped literal decimal point, since a . by itself normally means “any character”. The ? after it indicates it’s optional; it may or may not exist. This is to allow for numbers that may or may not have a decimal point.
%d+
%d again indicates a digit, but this time it has a + after it. This is a greedy repeating sequence, meant to match all the numbers after a decimal point (or just all the numbers, if no decimal point exists in the number).
Why use + instead of * for this one? Because + also requires “at least one”. Since the other two in this capture group were optional, this one makes sure that there is at least one digit.
The non-greedy alternative of - would almost work, except it would only match the first number after the decimal, whereas we want all of the numbers.
Greedy vs non-greedy just means that if the pattern could match part of a string in more than one way, it will choose either the shortest (-) or longest (+) match.
So in the end, this matches a number between <!-- and --> that is formatted as something like 1234, 12.34, .1234, or 1234. and returns it to the number variable. The tagStart and tagEnd are the index of the first and last characters matched in the pattern.