How Do I Make the Player's Name Bold in a String?

Hello! I’m trying to make the player’s name bold and a different color inside a dialogue system, how do I go about making that? Down below is the script and the format of the dialogue system:

Script:

local player = game.Players.LocalPlayer

local function typeWriter(s)
	local newString
	
	for i = 0, s:len(), 1 do
		newString = s:sub(1, i)
		
		if s:sub(i - 1, i -1) == "," then
			wait(0.4)
		elseif s:sub(i - 1, i -1) == "." then
			wait(0.6)
		elseif s:sub(i - 1, i -1) == "!" then
			wait(0.6)
		elseif s:sub(i - 1, i -1) == "?" then
			wait(0.6)
		end
		
		script.Parent.Frame:WaitForChild("TextLabel").Text = newString
		
		wait(0.01)
	end
end

local function playingType()
	typeWriter("Hello "..(player.Name).."! It's nice to see you playing this game!")
end

playingType()

Screenshot:

image

(If this is in the wrong category on the DevForum, then I’m sorry. I still see this as Scripting Support.)

1 Like
1 Like

It doesn’t work for me. I keep getting a red line under this character: <.

Roblox’s RichText won’t work with this instance, as you are trying to do a type writer effect, and so the markups would not be applied until the section of text is complete.

I’d recommend you check out Defaultio’s custom RichText. It’s a few years old, but last time I used it, it worked well.

1 Like

I fixed it. I had placed in the wrong part. BUT, when I play the game, it shows the format typing and then makes it bold, if that makes sense.

Yo. I’ve used Rich Text Markup for years now, and it’s not exactly the most performant solution since there’s so many Objects created so quickly within a short duration. If you’re not careful, you can easily freeze your game due to this limitation.

Mind sending a picture our way? (and the string you provided for the TextLabel)

It is because you’re iterating through the formatting character sequence. You should instantly place these in when you get to the username

I’ve since written my own RichText system, however, for their use case, I don’t see them using it being a massive problem.

This is what I mean. The text first shows the Rich Text format, and then actually applies the boldness.
image

image

This is discount bin way to fix the issue. I could not think of a better way at the moment. :grimacing:

I suggest trying the module that got linked over this awful code:

What it does is splits words found inside and into a ton of characters wrapped between and then writes them…

local function Typer(InputString)
	local Words = {}
	
	for Match in InputString:gmatch("%<?b?%>?%w+%<?%/?b?%>?") do
		local IsBold = not not Match:find("%<?b%>%w+%<%/b")
		
		Match = Match:gsub("%<b%>", ""):gsub("%<%/b%>", "")
		table.insert(Words, {Match, IsBold})
	end
	
	local Letters = {}
	
	for Index, Info in pairs(Words) do
		local Word = Info[1]
		local IsBold = Info[2]
		
		for Letter in Word:gmatch(".") do
			if (IsBold) then
				Letter = "<b>" .. Letter .. "</b>"
			end
			
			table.insert(Letters, Letter)
		end
		
		table.insert(Letters, " ")		
	end
	
	for i, Letter in pairs(Letters) do
		Label.Text = Label.Text .. Letter
		
		wait(.1)
	end
end

Typer("My name is <b>" ..  Client.Name .. "</b>. How are you?")

Should I just copy and paste this from my code? If you don’t mind.

You will have to modify some things probably, but if you wish, go ahead. Tread carefully though.

What does this part of the code do? I just need to change the part that says Label.

	for i, Letter in pairs(Letters) do
		Label.Text = Label.Text .. Letter

		wait(.1)
	end

Label is a TextLabel that displays the text.

1 Like

WOW! It works perfectly. The only problem is that it doesn’t implement punctuation.

oh, just modify the pattern a bit.

You will want to substitute %w+ for something that fits your case. Maybe %w+?%.?%??%!?

I’m not too sure. I don’t like string patterns that much.

Hmm. Not understanding it too much. So, to separate pieces of punctuation I just type something, like, an exclamation mark between ?% ?%