Iterating letters/characters in a string in real time

Im using a for i loop to make the typewriter effect and want to make waits based on characters and am lost on how i would go about doing it.

Example

" " = wait(1)
“a” = wait(.01)

Put this inside, ServerScriptService:

-- Change this Variable Below to your TextLabel
local DesiredTextLabel = game.StarterGui.ScreenGui.TextLabel

function TypeWrite(str: string) -- Argument looks for a string
	DesiredTextLabel.Text = str
	DesiredTextLabel.MaxVisibleGraphemes = 0
	for i = 1,str:len() do -- Gets the Length of the String
		wait(.2)
		DesiredTextLabel.MaxVisibleGraphemes += 1
	end

end

TypeWrite("Hello! This is an Example of a Typewriter effect!") 
-- This String inside is being applied to "str"

I know how to make a typewriter effect ive already done that

im trying to change this wait time depending on what letter is being sent through the typewriter effect

im not sure why you would want to do that, Can you explain?

Assuming you mean a random wait, do this:

function TypeWrite(str: string)
	game.StarterGui.ScreenGui.TextLabel.Text = str
	game.StarterGui.ScreenGui.TextLabel.MaxVisibleGraphemes = 0
	for i = 1,str:len() do
		local RandomChance = math.random(1, 25)
		task.wait(.1)
		game.StarterGui.ScreenGui.TextLabel.MaxVisibleGraphemes += 1
		if RandomChance == 10 then
			task.wait(0.4)
		elseif  RandomChance == 25 then
			task.wait(1)
		end
	end

end

repeat
TypeWrite("Hello!, This is an Example of a TypeWriter, This is going to repeat until you see the effects")
until false

Edit: Im dumb

Its to apply different pitches and waits for each letter (imagine animal crossing talking sounds) And youre still kinda missing what im going for

image
{ } meaning whatever waits i’d apply
im just using letters and spaces as examples here id go through ever different letter but just focusing on these two values are easier

You could use string:sub(i, i) to check if the current character is one of the characters that needs a modified wait. So for example:

local shortWait = {' '}

for i = 1, text:len() do
    local waitTime = 1
    if table.find(shortWait, text:sub(i, i)) --[[in this case it would be just the character that appears at the position of i]] then
        waitTime = 0.1
    end
    task.wait(waitTime)
    label.MaxVisibleGraphemes = i
end

If you want per-character waits, you could probably use the character as the index and the wait time as the value, so something like:

local modifiedWaits = {
    [' '] = 0.1;
}
-- ...
local waitTime = modifiedWaits[text:sub(i, i)] or 1
1 Like

Create a table with each letter corresponding to a wait time, and then make it so that the letters you leave out of the table use some kind of default wait time.

Demo:

where default wait time is 0.1, "a" = 0.2, "x" = 0.5, and spaces have zero wait time.

letter wait demo.rbxm (4.1 KB)

This is very basic, so it’s only programmed to handle waits for single characters.

To get the typing effect you want, try making repeated characters, like “ll” in hello, faster. Make the spacebar very fast, make capital letters slower, and make keys further from D, F, J, and K slower. Keys that you type with your less dominat fingers will be slightly slower. Also, people tend to think in between words, so add a small random delay after every few words.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.