Edit Chat Message

Hey everyone!
I’m trying to make a grammar fixer so that when someone forgets to use punctuation or capitalize their sentences it will automatically fix the message for them. Currently, I am just re sending the corrected message, but I was wondering if there was a way to edit the original message. Here is my current code.

local ChatS = game:GetService('Chat')

local function split(s,d)
    d = d or ' ';
    local t = {};
    s:gsub('([^'..d..']+)',function(c) t[#t+1] = c end);
    return t;
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Player.Chatted:Connect(function(Message)
			local fromPlayerId = Player
			local lastletter = string.sub(Message, -1)
			local firstletter = string.sub(Message,1,1)
			local restofmessage = string.sub(Message, 2, string.len(Message))
			local SplitMessage = split(Message)
			--if #SplitMessage >= 2 then
				if lastletter ~= "." and lastletter ~= "!" and lastletter ~= "?" then
					wait(0.5)
					if string.upper(firstletter) ~= firstletter then
						local NewMessage = string.upper(firstletter) .. restofmessage .. ".*"
						local filteredTextResult = ChatS:FilterStringForBroadcast(NewMessage, fromPlayerId)
						print(filteredTextResult)
						if string.sub(filteredTextResult, -1) ~= "#" or string.sub(filteredTextResult, 1,1) ~= "#" then
							ChatS:Chat(Character, filteredTextResult, "White")
						end
					else
						if #SplitMessage >= 2 then
							local NewMessage = Message .. ".*"
							local filteredTextResult = ChatS:FilterStringForBroadcast(NewMessage, fromPlayerId)
							if string.sub(filteredTextResult, -1) ~= "#" then
								ChatS:Chat(Character, filteredTextResult, "White")
							end
						end
					end
				elseif string.upper(firstletter) ~= firstletter then
					local NewMessage =  string.upper(firstletter) .. restofmessage .. "*"
					local filteredTextResult = ChatS:FilterStringForBroadcast(NewMessage, fromPlayerId)
					if string.sub(filteredTextResult, 1,1) ~= "#" then
						ChatS:Chat(Character, filteredTextResult, "White")
					end
				end
			--end
		end)
	end)
end)

Thanks everyone!

1 Like

You would need to copy the chat scripts from the chat service and edit them to integrate this into them.

2 Likes

You need to make own chat gui, so make input that input will be input of the script and output where the script will print output

1 Like

I wouldn’t recommend doing this mainly because your code can’t assume the context or meaning of what people are trying to say. It generally sounds like a pointless endeavour as well. It’s not my system or my place though, I’m just dropping down what I think. I hate when code does this to my messages.

If you’d still like to go ahead with this, this is something that you’ll need to hook up with the Lua Chat System. This can either be through a custom module under ClientChatModules.MessageCreatorModules or use of Chat.RegisterChatCallback.

1 Like

GUI that is cloned to all players by the Server.

Server Script that finds the message and changes the message for all clients.

If the chat is given to clients by the server, then the server can interact with it.

Make sure to use RemoteEvents and RemoteFunctions for Client-Server compatibility.

Perfectly described. For example i am cz and it is taging czech translation of word “but” and more autoediting is less qualit of chat because it less say what player want to say

As a note, do not forget you still have to filter your edited message. (The one provided by .Chatted is not filtered)