Is their a way to Delete a Message in the Chat?

The Title speaks for itself, Is their a way to Delete a Message in the Chat?

4 Likes

The manual way:
the chat messages in chat UI are client-side this means you will have to use a local script
chat messages are located in:

game.Players.LocalPlayer.PlayerGui.Chat.Frame.ChatChannelParentFrame.Frame_MessageLogDisplay.Scroller 

and all of them are named “Frame”(and the default chat message)

so you have to use other ways to “guess” the messages like

local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
--you may need to wait for any of the objects in the path
local messages = PlayerGui.Chat.Frame.ChatChannelParentFrame.Frame_MessageLogDisplay.Scroller 

--chat messages have a huge amount of spaces at the start(for some reason)
function removeSpaces(message) 
	local result = message 
	local length = message:len()
	for i = 1, length do 
		if result:sub(1, 1) == " " then 
			result = result:sub(2, length)
		else 
			break 
		end
	end
	return result 
end

for i, message in pairs(messages:GetChildren()) do --loop through current messages
	if not message:IsA("Frame") then continue end
	if not message:FindFirstChild("TextLabel") then continue end 
	
	local Button = message.TextLabel:FindFirstChild("TextButton")
	if Button then 
		print("actual chat message")
		local text = Button.Text
		local username = text:sub(2, text:len()-2) --cut out "[" and "]:
		print("user:", username)
	else 
		print("Probably a system message")
	end 

	local messageText = removeSpaces(message.TextLabel.Text)
	print("the message:", messageText)
	
	--actually "delete" the message(it will be done client-side other users will still be able to see it)
	message:Destroy() 
end

you can use checks like that so for example, delete all the messages from a user, or filter specific words

if you want a solution that prevents messages from being sent in the first place I suggest you check this post:

3 Likes

I may have wrote that post with the idea of preventing a chat message from being sent but there’s two reasons why I say in this post that you should prevent it instead of deleting it:

  1. There’s no clean way to delete a chat message. Deleting Gui elements isn’t enough, there are still components in the Lua Chat System that hold onto the message that need to be cleared out.

  2. Most of your use cases for wanting to delete a chat message should actually be in the sense of not sending the chat message at all if it doesn’t pass a certain condition. You very rarely need to legitimately delete a chat message after it’s been posted.

The only clean, native way to remove a chat message is RemoveLastMessageFromChannel in the ChatChannelUI API, but because of the way the Lua Chat System is developed it’s practically inaccessible without redoing the part where it gets created. It also only removes the last message, not a target message, so it’s not ideal.

7 Likes