Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I filter my classic chat GUI?

Hi. I have a custom chat script that brings back the classic ROBLOX chat. The problem is it allows bad words to bypass the filter. How must I filter this chat? I've looked up a lot of methods and tutorials yet they're either too complex, don't fit in the chat's layout or aren't clear enough in explaining.

Link: https://www.roblox.com/library/4452495402/Classic-Roblox

Script:

local year = script.Parent:WaitForChild("Year").Value



--Code

if year == 2007 then
	script.Parent.ClickToChat.TextColor3 = Color3.new(0, 0, 0)
	script.Parent.ChatBar.BackgroundColor3 = Color3.new(255/255, 255/255, 255/255)
end

local Chat = { 
	ChatColors = {
		BrickColor.new("Bright red"),
		BrickColor.new("Bright blue"),
		BrickColor.new("Earth green"),
		BrickColor.new("Bright violet"),
		BrickColor.new("Bright orange"),
		BrickColor.new("Bright yellow"),
		BrickColor.new("Light reddish violet"),
		BrickColor.new("Brick yellow"),
	};

	Gui = nil,
	Frame = nil,
	RenderFrame = nil,
	TapToChatLabel = nil,
	ClickToChatButton = nil,
	Templates = nil,
	EventListener = nil,
	MessageQueue = {},
	
	Configuration = {								
		FontSize = Enum.FontSize.Size12,	
		NumFontSize = 12,
		HistoryLength = 20,
		Size = UDim2.new(0.38, 0, 0.20, 0),
		MessageColor = Color3.new(1, 1, 1),
		AdminMessageColor = Color3.new(1, 215/255, 0),
		XScale = 0.025,
		LifeTime = 45,
		Position = UDim2.new(0, 2, 0.05, 0),
		DefaultTweenSpeed = 0.15,							
	};
	
	CachedSpaceStrings_List = {},
	Messages_List = {},
	MessageThread = nil,
	TempSpaceLabel = nil,
}

function GetNameValue(pName)
	local value = 0
	for index = 1, #pName do 
		local cValue = string.byte(string.sub(pName, index, index))
		local reverseIndex = #pName - index + 1
		if #pName%2 == 1 then 
			reverseIndex = reverseIndex - 1			
		end
		if reverseIndex%4 >= 2 then 
			cValue = -cValue 			
		end 
		value = value + cValue 
	end 
	return value%8
end 

function Chat:ComputeChatColor(pName)
	return self.ChatColors[GetNameValue(pName) + 1].Color
end 

function Chat:UpdateQueue(field, diff)	 				
	for i = #self.MessageQueue, 1, -1 do 			
		if self.MessageQueue[i] then 						
			for _, label in pairs(self.MessageQueue[i]) do 						
				if label and type(label) ~= "table" and type(label) ~= "number" then					
					if label:IsA("TextLabel") or label:IsA("TextButton") then 	
						if diff then 
							label.Position = label.Position - UDim2.new(0, 0, diff, 0) 
						else											
							if field == self.MessageQueue[i] then 						
								label.Position = UDim2.new(self.Configuration.XScale, 0, label.Position.Y.Scale - field["Message"].Size.Y.Scale , 0)
							else 							
								label.Position = UDim2.new(self.Configuration.XScale, 0, label.Position.Y.Scale - field["Message"].Size.Y.Scale, 0)							
							end  
							if label.Position.Y.Scale < -0.01 then 							
								label.Visible = false 						
								label:Destroy() 
							end 
						end 
					end 
				end 						
			end 
		end 
	end	
end 

function Chat:ComputeSpaceString(pLabel)
	local nString = " "
	if not self.TempSpaceLabel then 
		self.TempSpaceLabel = self.Templates.SpaceButton
		self.TempSpaceLabel.Size = UDim2.new(0, pLabel.AbsoluteSize.X, 0, pLabel.AbsoluteSize.Y); 
	end
	self.TempSpaceLabel.Text = nString
	while self.TempSpaceLabel.TextBounds.X < pLabel.TextBounds.X do 
		nString = nString .. " "
		self.TempSpaceLabel.Text = nString 				
	end 
	nString = nString .. " "
	self.CachedSpaceStrings_List[pLabel.Text] = nString 
	self.TempSpaceLabel.Text = ""
	return nString	
end


function Chat:UpdateChat(cPlayer, message)
	local messageField = {["Player"] = cPlayer,["Message"] = message}	
	if coroutine.status(Chat.MessageThread) == "dead" then 				
		table.insert(Chat.Messages_List, messageField)				
		Chat.MessageThread = coroutine.create(function ()
			for i = 1, #Chat.Messages_List do 	
				local field = Chat.Messages_List[i]																														
				Chat:CreateMessage(field["Player"], field["Message"]) 						
			end 
			Chat.Messages_List = {}									
		end)
		coroutine.resume(Chat.MessageThread)
	else 
		table.insert(Chat.Messages_List, messageField)
	end 
end 

function Chat:CreateMessage(cPlayer, message)		
	local pName = cPlayer ~= nil and cPlayer.Name or "GAME"	
	message = message:gsub("^%s*(.-)%s*$", "%1")		
	self.MessageQueue[#self.MessageQueue] = (#self.MessageQueue > self.Configuration.HistoryLength) and nil or self.MessageQueue[#self.MessageQueue]
	local pLabel = self.Templates.pLabel:clone()
	pLabel.Name = pName;
	pLabel.Text = pName .. ";";
	pLabel.Parent = self.RenderFrame;			
	pLabel.TextColor3 = (cPlayer.Neutral) and Chat:ComputeChatColor(pName) or cPlayer.TeamColor.Color 
	local nString = self.CachedSpaceStrings_List[pName] or Chat:ComputeSpaceString(pLabel)
	local mLabel = self.Templates.mLabel:clone()
	mLabel.Name = pName .. " - message";					
	mLabel.TextColor3 = Chat.Configuration.MessageColor;					
	mLabel.Text = nString .. message;
	mLabel.Parent = self.RenderFrame;	
	local heightField = mLabel.TextBounds.Y	
	mLabel.Size = UDim2.new(1, 0, heightField/self.RenderFrame.AbsoluteSize.Y, 0)	
	pLabel.Size = mLabel.Size
	local yPixels = self.RenderFrame.AbsoluteSize.Y
	local yFieldSize = mLabel.TextBounds.Y
	local queueField = {}	
	queueField["Player"] = pLabel 
	queueField["Message"] = mLabel 
	queueField["SpawnTime"] = tick()
	table.insert(self.MessageQueue, 1, queueField)		
	Chat:UpdateQueue(queueField)
end

function Chat:CreateChatBar()	
	self.ClickToChatButton = self.Gui:WaitForChild("ClickToChat")
	self.ChatBar = self.Gui:WaitForChild("ChatBar")
	local mouse = game.Players.LocalPlayer:GetMouse()
	mouse.KeyDown:connect(function (key)
		if key:byte() == 47 or key == "/" then
			self.ClickToChatButton.Visible = false
			if year > 2007 then
				self.ChatBar.BackgroundColor3 = Color3.new(255/255, 255/255, 255/255)
			end
			self.ChatBar:CaptureFocus()
		end
	end)
	self.ClickToChatButton.MouseButton1Click:connect(function ()
		self.ClickToChatButton.Visible = false
		if year > 2007 then
			self.ChatBar.BackgroundColor3 = Color3.new(255/255, 255/255, 255/255)
		end
		self.ChatBar:CaptureFocus()
	end)	
end

function Chat:PlayerChat(message)
	local m = Instance.new("StringValue")
	m.Name = "NewMessage"
	m.Value = message
	m.Parent = game.Players.LocalPlayer
	game:GetService("Debris"):AddItem(m, 2)
end

function Chat:CreateGui()
	self.Gui = script.Parent
	self.Frame = self.Gui:WaitForChild("ChatFrame")
	self.RenderFrame = self.Frame:WaitForChild("ChatRenderFrame")
	self.Templates = self.Gui:WaitForChild("Templates")	
	Chat:CreateChatBar()
	self.ChatBar.FocusLost:connect(function(enterPressed)
		if year > 2007 then
			self.ChatBar.BackgroundColor3 = Color3.new(64/255, 64/255, 64/255)
		end
		if enterPressed and self.ChatBar.Text ~= "" then 
			local cText = self.ChatBar.Text
			if string.sub(self.ChatBar.Text, 1, 1)  == "%" then 
				cText = "(TEAM) " .. string.sub(cText, 2, #cText)
			end
			Chat:PlayerChat(cText)											
			if self.ClickToChatButton then 
				self.ClickToChatButton.Visible = true 
			end 
			self.ChatBar.Text = ""	
			self.ClickToChatButton.Visible = true							
		end	
	end)	
end

function Chat:TrackPlayerChats()
	local function chatRegister(p)
		p.ChildAdded:connect(function (obj)
			if obj:IsA("StringValue") and obj.Name == "NewMessage" then
				if game.Players.LocalPlayer.Neutral or p.TeamColor == game.Players.LocalPlayer.TeamColor then
					Chat:UpdateChat(p, obj.Value)
				end
			end
		end)
	end
	for _,v in pairs(game.Players:GetPlayers()) do
		chatRegister(v)
	end
	game.Players.PlayerAdded:connect(chatRegister)
end

function Chat:CullThread()
	Spawn(function ()
		while true do 
			if #self.MessageQueue > 0 then 
				for _, field in pairs(self.MessageQueue) do 				
					if field["SpawnTime"] and field["Player"] and field["Message"] and tick() - field["SpawnTime"] > self.Configuration.LifeTime then 
						field["Player"].Visible = false 
						field["Message"].Visible = false 
					end 
				end 
			end 
			wait(5)
		end
	end)
end

function Chat:Initialize()			
	Chat:CreateGui()
	Chat:TrackPlayerChats()
	self.MessageThread = coroutine.create(function () end)
						 coroutine.resume(self.MessageThread)
	Chat:CullThread()
end

Chat:Initialize()

2 answers

You could always use FilterStringAsync as shown here note This is an example script so you'll want to apply this to yours.

local textService = game:GetService("TextService")
local textToFilter = nil -- This is the text you want to filter

local function filterString(str, playerId)
	local result = ""
	local success, failure = pcall(function()
		textService:FilterStringAsync(str, playerId)
	end)

	if success then
		return result
	end
end -- It is important to call a pcall here because sometimes the method won't work properly because it makes web requests.

print(filterString(textToFilter, userId))
-- Be sure your userId is an actual UserId.

You can read more about it here.

12 comments

0

Alright, where do I put this script?

0

What line are you sending the chat message?

0

I didn't make the script (GASP, FREE MODELS!!!) but I'll check and see

0

please be patient as I'm new to scripting, and as such, am a bit dumb

0

It looks to be near line 117.

0

now the chat doesn't work at all, i got error "Players.Mycran.PlayerGui.ClassicChat.ChatScript:268: Expected 'end' (to close 'function' at line 116), got ; did you forget to close 'then' at line 249?"

0

Now the chat doesn't appear at all, only shows the bar which you can type in, but no chat box for the text to appear. No error output.

0

I'm not sure than. I didn't write the script, you didn't write the script, find another custom chat box to use that will filter the messages or change the default one to look how you want it to look.

0

Thanks for ur help, tho

0

mhm

Are you testing in studio? Messages don't get filtered in studio. What suspectshot108 said should work. :)

1 comment

0

I tested in normal ROBLOX, there is still no filtering.

This discussion is preserved as it was. New answers, comments, and votes are closed.