How to yield string if character is detected?

I’m writing a function that typewrites a string but if it detects “,” it will yield for a bit, then continue.
But after “,” is detected it will just slow the typewriting speed. Anybody know how to fix this?

function Utilities.TypeWriteText(Data)
	for i = 1,#Data.Text,1 do
		Data.Object.Text = string.sub(Data.Text, 1, i)
		
		if Data.SoundEnabled then
			AudioContainer:WaitForChild("MenuBlip"):Play()
		end
		
		if Data.Object.Text:match(",") then
			task.wait(0.3)
		else
			task.wait(Data.Time)
		end
	end
end

Hello! The reason it yields even after passing the comma is because the string.match() function checks if a comma is inside the text, which after passing it, will always return true. What you should do is get the current character that the typewriter is adding, you can do this with string.sub(text,i,i). You can turn this into a variable and check if the character is a comma or whatnot. I have my own typewriting script that uses graphemes instead of adding characters, and this is better because it allows you to add rich-text formatting. Below is the module script that I made (you can use it if you like!)

local module = {}
function module:CreateDialog(text, label, sound, lengthTime, commaTime, periodTime)
	label.Text = text
	label.MaxVisibleGraphemes = 0
	for i,v in utf8.graphemes(label.ContentText) do
		local char = label.ContentText:sub(i,i)
		label.MaxVisibleGraphemes = i
		if sound then
			if char ~= " " then
				local soundClone = sound:Clone()
				soundClone.Parent = label
				soundClone.PlayOnRemove = true
				soundClone:Destroy()
			end
		end
		if char == "," then
			task.wait(commaTime)
		elseif char == "." then
			task.wait(periodTime)
		else
			task.wait(lengthTime)
		end
	end
end
return module

This script is a bit old, so that’s why it’s not in a table, but it should be easy to format it that way!
I hope this helps!

This worked beautifully! Thank you so much!

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