Rich text escape forms dont work when changed via code

  1. What do you want to achieve?
    I want to change “>” to “&gt” (with the ; at the end ofc) and make it render correctly

  2. What is the issue? Include screenshots / videos if possible!
    image

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    yes. i have looked but there isn’t a SINGLE post about this. no matter what I tried it just doesn’t work

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local StaticSyntax = {
	["local"] = '<font color="#ff0000">local</font>',
	["cool"] = '<font color="#00ff00">cool</font>',
	["blue"] = '<font color="#0000ff">blue</font>',
	['"(.-)"'] = '<font color="#55ff7f">&quot;%1&quot;</font>',
}

local SyntaxReplacing = true

function ReturnReplaceSyntax()
	local Code:string = script.Parent.Text
	
	if SyntaxReplacing == false then return Code end
	
	Code = Code:gsub("<","&lt;")
	Code = Code:gsub(">","&gt;")
	Code = Code:gsub('"',"&quot;")
	Code = Code:gsub("'","&apos;")
	Code = Code:gsub("&","&amp;")
	
	for word,replace in pairs(StaticSyntax) do
		Code = Code:gsub(word,replace)
	end
	
	return Code
end

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	script.Parent.TextLabel.Text = ReturnReplaceSyntax()
end)

Please help. It works if I put it in manually through properties but does NOT work when I change via code

EDIT:
the “&” character works but only “>” and “<” are broken.
also here is the updated script

local StaticSyntax = {
	["local"] = '<font color="#ff0000">local</font>',
	["cool"] = '<font color="#00ff00">cool</font>',
	["blue"] = '<font color="#0000ff">blue</font>',
}

local DynamicSyntax = {
	['"(.-)"'] = '<font color="#55ff7f">&quot;%1&lt;&quot;</font>',
	["'(.-)'"] = '<font color="#55ff7f">&apos;%1&lt;&apos;</font>',
}

local SyntaxReplacing = true

function ReturnReplaceSyntax()
	local Code:string = script.Parent.Text
	
	if SyntaxReplacing == false then return Code end
	
	Code = Code:gsub("<","&lt;")
	Code = Code:gsub(">","&gt;")
	Code = Code:gsub("&","&amp;")
	
	for word,replace in pairs(DynamicSyntax) do
		Code = Code:gsub(word,replace)
	end
	
	for word,replace in pairs(StaticSyntax) do
		Code = Code:gsub(word,replace)
	end
	
	return Code
end

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	script.Parent.TextLabel.Text = ReturnReplaceSyntax()
end)

Escape raw text first, then insert font tags.. never escape after that. I’ll make a test script for ya, give me a second.

--LocalScript in StarterPlayerScripts

local screenGui = Instance.new("ScreenGui", game.Players.LocalPlayer.PlayerGui)
local inputBox = Instance.new("TextBox", screenGui)
inputBox.Size = UDim2.new(0,400,0,100)
inputBox.Position = UDim2.new(0,50,0,50)
inputBox.ClearTextOnFocus = false

local displayLabel = Instance.new("TextLabel", screenGui)
displayLabel.Size = UDim2.new(0,400,0,100)
displayLabel.Position = UDim2.new(0,50,0,160)
displayLabel.RichText = true

local StaticSyntax = {
	["local"] = '<font color="#ff0000">local</font>',
	["print"] = '<font color="#00ffff">print</font>',
}

local DynamicSyntax = {
	['"(.-)"'] = '<font color="#55ff7f">&quot;%1&quot;</font>',
}

local function Escape(text)
	text = text:gsub("&","&amp;")
	text = text:gsub("<","&lt;")
	text = text:gsub(">","&gt;")
	return text
end

local function Render()
	local code = Escape(inputBox.Text)
	for pattern,replace in pairs(DynamicSyntax) do
		code = code:gsub(pattern, replace)
	end
	for word,replace in pairs(StaticSyntax) do
		code = code:gsub(word, replace)
	end
	return code
end

inputBox:GetPropertyChangedSignal("Text"):Connect(function()
	displayLabel.Text = Render()
end)

Use the input box to show that text in the lower box.

It’s old-school HTML-style escaping and tagging. There’s tons of references online, it’s not new or special to Roblox, just adapted to RichText.

1 Like

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