so, i’ve created a typewriter effect while i was messing around with the MaxVisibleGraphemes property of TextLabels. right now, my main goal is improving my programming skills, and i would like some feedback.
this is the project file:
typewriterText.rbxl (204.7 KB)
the source code, if you don't want to download the .rbxl file:
local textTable = {
"this is a simple typewriter effect. blah blah blah, yeah yeah. text.",
"hello world!",
"sometimes, i dream about cheese.",
"charging up my typewriter!!!",
".JJJJJJJJJJJJJJJJJJ.JJJJJJJJJJJJJJ",
"i just got here; the traffic was terrible. now, consider the following: AAAAGGHH! JEEEJJGH?!?! ok. that's all.",
"for every person who dreams of the electric lightbulb, there's the one who dreams of the atom bomb.",
"me personally, i wouldn't let that one slide",
"mild headache??"
}
local textLabel = script.Parent:WaitForChild("Frame").TextLabel
-- local runService = game:GetService("RunService")
local maxLength = 0
for _, text in ipairs(textTable) do
maxLength = math.max(maxLength, #text)
end
local sentencePauseCharacters = { ",", ":", ";", "-" }
local sentenceEndingCharacters = { ".", "?", "!" }
local fixedDuration = 3
local pauseDuration = 0.3 -- how long are the pauses?
local isPaused = false
local function autoText(object, text)
object.MaxVisibleGraphemes = 0
object.Text = text
local duration = fixedDuration * (#text / maxLength)
for i = 1, #text do
if isPaused then
repeat task.wait() until not isPaused
end
local char = text:sub(i, i)
if char ~= " " then
if table.find(sentencePauseCharacters, char) then
coroutine.wrap(function()
isPaused = true
script.typeWrite:Play()
task.wait(pauseDuration)
isPaused = false
end)()
elseif table.find(sentenceEndingCharacters, char) then
coroutine.wrap(function()
isPaused = true
script.pausedSFX:Play()
task.wait(pauseDuration * 1.75)
isPaused = false
end)()
end
end
object.MaxVisibleGraphemes = i
task.wait(duration / #text)
end
end
while task.wait(5) do
autoText(textLabel, textTable[math.random(1, #textTable)])
end
as of right now:
-
my script creates a typewriter effect by displaying randomly selected texts from a predefined list at regular intervals. it introduces dynamic typing speed, pauses for certain characters to make reading feel more natural, and it includes sound effects (which was used for debugging)
-
i’m wondering, is there any way to maximize efficiency? is my method performant?