Chat System: Am I using the best practice here?

I want my chat to have stylized chat bubbles, and the default bubble chat service doesn’t really match what I’m going for, it doesn’t align with my name tag. So I made a custom bubble chat within my name tag script. I don’t know how stable this is, or if I should use a different approach (Like, I was thinking of forking it from TextChatService.MessageReceived, and displaying it, if it’s the player’s message, that way I don’t have to reinvent the chat filter). I also don’t know how vulnerable this is to exploits.

I would also like to mention that there’s a character select as well, that spawns the character you’ve chosen into workspace under it’s name.

local Players = game:GetService("Players")
local TextService = game:GetService("TextService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

local logicFolder = ReplicatedStorage:WaitForChild("Util")
local AttachmentRely = logicFolder:WaitForChild("AttachmentReliant")
local NameTagTemplate = AttachmentRely:WaitForChild("Nametag")
local MsgTemplate = script:WaitForChild("Msg")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local head = character:FindFirstChild("Head")
		if not head then return end
		local nameTag = NameTagTemplate:Clone()
		nameTag.Parent = head
		nameTag.BillboardGui.User.Text = player.Name.." ("..character.Name..")"
		player.Chatted:Connect(function(message)
			local success, filteredMessage = pcall(function()
				return TextService:FilterStringAsync(message, player.UserId):GetNonChatStringForBroadcastAsync()
			end)
			if success and filteredMessage then
				local existingMsg = nameTag.BillboardGui:FindFirstChild("Msg")
				if existingMsg then existingMsg:Destroy() end
				local msg = MsgTemplate:Clone()
				msg.Name = "Msg"
				msg.Text = filteredMessage
				msg.Parent = nameTag.BillboardGui
				msg.Visible = true
				Debris:AddItem(msg, 3)
			end
		end)
	end)
end)
1 Like