Write System with colorful text for Dialogue

Hi, i’m working on a Dialogue System with the write effect. I want to implement the Text color using a rich text label in the write system.

This is the write System:

function DialogueSystem.CompileText(Dialogue:string,DialogueTextBox:TextLabel)
	local WRITETIME = .03
	
	local Connection = UIS.InputBegan:Once(function(input,Processed)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or 
			input.UserInputType == Enum.UserInputType.Touch then
			DialogueTextBox.Text = Dialogue
		end
	end)
	for i=1, #Dialogue do
		if DialogueTextBox.Text == Dialogue then
			return
		end
		DialogueTextBox.Text = Dialogue:sub(1,i)
		task.wait(WRITETIME)
	end
	Connection:Disconnect()
end

And this is the Color Text Function:

function TextHelper.ColorText(TextColor:Color3,Text:string)
	local r = math.round(TextColor.R*255)
	local g = math.round(TextColor.G*255)
	local b = math.round(TextColor.B*255)
	local rgb = "("..r..","..g..","..b..")"
	
	local NewText:string = "<font color=\"".."rgb"..rgb.."\">"..Text.."</font>"
	return NewText
end

Both scripts works fine if used separately, but when I use them in a Dialogue it colors the text only at the end (of course becuse the color string isn’t completed so it take the string as a normal text). How can I fix this?

It’s because when it has the typing animation, the colored part of the string isn’t completed. I think Defaultio’s RichText module has a fix for this so maybe try that

Is there no other way? The way you suggested could work but I would have to change a lot of things to implement it. Theoretically if I were to write the string that colors the text instantly and then write in it with my script I think the problem would be solved. But I don’t know how to do that, How can I make the script skip that part of text?

For the text to be colored you need the whole sentence to be finished, so you should use MaxVisibleGraphemes instead of string.sub. You will also need to use a function to get the text without the tags for color to get the proper length of the text:

local function removeTags(str) --this function will return plain text
	str = str:gsub("<br%s*/>", "\n")
	return (str:gsub("<[^<>]->", ""))
end

if DialogueTextBox.Text ~= Dialogue then
    DialogueTextBox.MaxVisibleGraphemes = 0
    DialogueTextBox.Text = Dialogue
    local length = #removeTags(Dialogue)

	for i=1, length do
		DialogueTextBox.MaxVisibleGraphemes = i
		task.wait(WRITETIME)
	end
end

2 Likes

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