Typewriter Effect Feedback

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?

2 Likes

This effect is very nice! I really like it! But the issue though, since I have no idea how to script, could you make it so that instead of the typewriter noise playing every punctuation, why don’t you play the sound every letter?

1 Like

thanks!

i only did the punctuation sounds to debug the punctuation’s timing, because at the time, when i would implement punctuation pauses, the pauses would be slughtly off and land on normal characters i’ve removed that portion and it now plays for every letter.

1 Like