How do I add more text to a text label and not completely change the text in a typewriter effect?

Hi I’m trying to create a typewriter effect where I have the option to add more text to the TextLabel instead of completely changing the text. One game that does this is Break In.

Here is my code in the localscript that changes the text currently:

local function setText(text, object, waitTime)
	object.Text = text
	
	local contentText = object.ContentText

	local count = 0
	for first, last in utf8.graphemes(contentText) do
		count += 1
		object.MaxVisibleGraphemes = count

		local currentGrapheme = string.sub(contentText, first, last)

		script.Parent.Sound:Play()

		task.wait(waitTime)
	end
end

game.ReplicatedStorage.Events.SetVisibility.OnClientEvent:Connect(function(visible)
	script.Parent.Parent.Visible = visible
end)

game.ReplicatedStorage.Events.Dialogue.OnClientEvent:Connect(function(text, transparency, waitTime, backgroundcolor)
	script.Parent.Parent.BackgroundTransparency = transparency
	script.Parent.Parent.BackgroundColor3 = backgroundcolor
	setText(text, script.Parent, waitTime)
end)
	object.Text = object.Text..text

try that

1 Like

it works but I also forgot to mention that I want it so that when the Dialogue event is called and when it’s adding text it only types the text I added and not the whole thing.

What do you mean by not the whole thing?

what I mean is I want the typewriter effect to type only the text I added to the string by using object.Text = object.Text..text not the entire string from the beginning every time

I understand your concern. Here’s how you’d approach it. Instead of putting count as 0, just put it as the current length. Should cover all cases.

local function addText(text, object, waitTime)
	local count = #object.Text
        object.Text ..= text
	
	local contentText = object.ContentText

	for first, last in utf8.graphemes(contentText) do
		count += 1
		object.MaxVisibleGraphemes = count

		local currentGrapheme = string.sub(contentText, first, last)

		script.Parent.Sound:Play()

		task.wait(waitTime)
	end
end

local function clearText(text, object, waitTime)
	
	local contentText = object.ContentText

	while object.MaxVisibleGraphemes > 0 do
		object.MaxVisibleGraphemes -= 1
		script.Parent.Sound:Play()
		task.wait(waitTime)
	end
       
    object.Text = ""

end

game.ReplicatedStorage.Events.SetVisibility.OnClientEvent:Connect(function(visible)
	script.Parent.Parent.Visible = visible
end)

game.ReplicatedStorage.Events.Dialogue.OnClientEvent:Connect(function(text, transparency, waitTime, backgroundcolor)
	script.Parent.Parent.BackgroundTransparency = transparency
	script.Parent.Parent.BackgroundColor3 = backgroundcolor
	addText(text, script.Parent, waitTime)
end)

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