Custom Chat GUI, but it lacks color for players names

This is my script:

UserInputService = game:GetService('UserInputService')
event = game.ReplicatedStorage:WaitForChild('PlayerChatted')

function strip(s)
	return (s:gsub('^%s*(.-)%s*$', '%1'))
end

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.Slash then
				script.Parent.Parent.Message.MessageBox:CaptureFocus()
			end
		end
	end
end)

script.Parent.Parent.Message.MessageBox.FocusLost:Connect(function(enter)
	if enter then
		local trimmed = strip(script.Parent.Parent.Message.MessageBox.Text)
		if trimmed ~= '' then
			event:FireServer(trimmed)
		end
		script.Parent.Parent.Message.MessageBox.Text = ''
	end
end)

event.OnClientEvent:Connect(function(filtered)
	local textlabel = Instance.new('TextLabel', script.Parent)
	textlabel.BackgroundTransparency = .8
	textlabel.BorderSizePixel = 0
	textlabel.BackgroundColor3 = Color3.new(1, 1, 1)
	textlabel.Position = UDim2.new(0, 0, 1, 0)
	textlabel.Size = UDim2.new(1, 0, 0.1, 0)
	textlabel.Font = Enum.Font.Cartoon
	textlabel.TextColor3 = Color3.new(1, 1, 1)
	textlabel.TextSize = 14
	textlabel.TextXAlignment = Enum.TextXAlignment.Left
	textlabel.Text = filtered

	for i, v in pairs(script.Parent:GetChildren()) do
		if not (v == script) then
			v:TweenPosition(
				UDim2.new(0, 0, v.Position.Y.Scale - 0.1, 0),
				Enum.EasingDirection.InOut,
				Enum.EasingStyle.Linear,
				0,
				false
			)
			if v.Position.Y.Scale <= 0 then
				v:Destroy()
			end
		end
	end
end)

(This script is in the custom GUI)
How would I make different colors for player names? I’ve tried looking everywhere for it and I cant find it.

Here’s the other script that resides in ServerScriptService:

event = game.ReplicatedStorage:WaitForChild('PlayerChatted')

event.OnServerEvent:Connect(function(player, message)
	local filtered = ('%s: %s'):format(
	player.Name, game.Chat:FilterStringForBroadcast(message, player)
)
event:FireAllClients(filtered)

local x = 2 + message:len()/15
local y = x/2
local billboard = game.ReplicatedStorage:WaitForChild('ChatBillboard'):Clone()
billboard.Bubble.Display.Text = game.Chat:FilterStringForBroadcast(message, player)
billboard.Adornee = player.Character.Head
billboard.Size = UDim2.new(x, 0, y, 0)
game.Debris:AddItem(billboard, 4)

for i, v in pairs(player.Character.Head:GetChildren()) do
		if v.Name == 'ChatBillboard' and v.ClassName == 'BillboardGui' then
			v.StudsOffsetWorldSpace = Vector3.new(0, v.StudsOffsetWorldSpace.Y + y, 0)
			v.Size = UDim2.new(v.Size.X.Scale * 0.7, 0, v.Size.Y.Scale * 0.7, 0)
			if v.Size.X.Scale <= 1 then
				v:Destroy()
			end
		end
	end
	billboard.Parent = player.Character.Head
end)

Have you looked into rich text markup yet?

I have but I don’t believe I would be able to use multiple colors for it, which I want to use for different players.

What do you mean? Rich text can have multiple colors, font sizes, stroke colors and more, you would just need to make sure to sanitize players names and messages.
Simple color rich text

local hexColorCode = "#ff00ff"
local tagColorCode = "#00ff00"
local textlabel
textlabel.TextColor3 = Color3.new(1, 1, 1) -- or TextColor idk
textlabel.RichText = true
textlabel.Text = `<font color="{tagColorCode}">[{tag}]</font> <font color="{hexColorCode}">{name}: </font>{message}`

this would make the name pink and the message white. You can now just change the hexColorCode and the name color will change. You can include multiple of those as you can see in my example I included a tag.

(the syntax highlight doesnt work on here for `` because its a luau thing only)

Huh, I didn’t know that, kudos to you!

1 Like

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