How to underline a text using RichText when MouseEnter?

I want my textbutton’s text to be underlined when player hovers over the textbutton. I want the text to be un-underlined when player’s mouse leaves the button.

I’ve so far made this but it doesn’t work.

for i, v in pairs (topbarFrame:GetChildren()) do
	if v:IsA("TextButton") then
	--		local modifiedText = 
			v.Text = "<u>"..v.Text.."<u>"
			v.Text = tostring(v.Text)

The format is like this (you forgot the backslash at the end):


Also, tostring doesn’t remove the tags for you. If your text is always the same, store the string in a variable, and add the tags right then and there in the MouseEnter event:

local text = "Hello!"

--in the MouseEnter
v.Text = '<u>' .. text .. '</u>'

--in the MouseLeave
v.Text = text

But if your text changes, you can use string patterns and gsub to dynamically filter out tags:

--gsub replaces all occurrences of the string with the given repl string

--in the MouseLeave
v.Text = v.Text:gsub('</?.>', '') --replace <x> or </x> with empty string

If you don’t understand patterns, I suggest you look at the string pattern reference. But basically:

  • captures < first using <
  • captures 1 or less of / (since the max is 1 for closing tags, 0 for starting) using /?
  • captures all characters using . (since it can be u, font color = blah, font family = blah, or anything really)
  • captures > last using >
1 Like