Typewriter effect not working when user clicks to skip effect

The reason that it isn’t working is because you never disconnect the function above. Meaning that every time it’s clicked it will call that function even after the for loop has finished. I am going to supply code that I believe should fix it, but it also uses the new property that Roblox recently introduced. Typewriter Effect: New property MaxVisibleGraphemes (Live) - #3 by PysephDEV

This should work if you just replace the current typewrite function with both of these functions, though with your use case the removeTags function might not be necessary.

local function removeTags(str)
	-- replace line break tags (otherwise grapheme loop will miss those linebreak characters)
	str = str:gsub("<br%s*/>", "\n")
	return (str:gsub("<[^<>]->", ""))
end

local function typewrite(object, text)
	object.Text = text
	object.MaxVisibleGraphemes = 0
	local displayText = removeTags(script.Parent.LocalizedText)
	
	local tempConnection --have to declare here so I can call it in the function
	tempConnection = clickDetector.MouseButton1Up:Connect(function()
		object.MaxVisibleGraphemes = -1 --Shows the whole string
		tempConnection:Disconnect() --Cleans up the loose ends in the script.  This is essentially all you were missing that caused the error before.
	end)

	local index = 0
	for first, last in utf8.graphemes(displayText) do
		if object.MaxVisibleGraphemes == -1 then break end --Terminate the loop if it's been skipped
		local grapheme = displayText:sub(first, last) 
		index += 1
		-- Uncomment this statement to get a reveal effect that ignores spaces.
		-- if grapheme ~= " " then
		script.Parent.MaxVisibleGraphemes = index
		wait()
		-- end
	end
end

But if you don’t want to use the new property, you can change this

clickDetector.MouseButton1Up:Connect(function()
	textLabel.Text = text
	brokenLoop = true
end)

to this

local tempConnection
tempConnection = clickDetector.MouseButton1Up:Connect(function()
	textLabel.Text = text
	brokenLoop = true
    tempConnection:Disconnect()
end)
2 Likes