Issue with Lua chat and custom remove message function

In my minigame place, I moved my message system (it does stuff like show intermission timer, etc) to the lua chat and also added a custom remove message function to it that can remove a message given the messages text. This is so the messages don’t clutter the chat, especially with the timers since they need to be updated every second. I created a custom channel module to do this stuff in.

To call the function, I do:
mainChannel:RemoveLastSystemMessage(lastSysMessage,"MGEngine")

where mainChannel is the channel made via ChatService:AddChannel, MGEngine is the name of the channel and lastSysMessage is the text. (lastSysMessage is a variable in my script for the last system message sent. It’s updated after every time a new message is sent).

Here’s how it works in order of execution (put inside a pastebin since otherwise I think the post will be way too long. Also, the pastebin isn’t one script if that isn’t obvious. It’s the functions taken from the script/module that’s shown in the comment above each snippet in order of when they executed.)

But the issue is that after a while, the chat just… stops. There’s no errors, not warnings. It just stops. No system messages appear. If a player sends a message, it doesn’t appear. It starts off fine, but after a few minutes the chat will just abruptly die.

I’m new to the whole lua chat thing so my implementation of a removemessage function probably isn’t ideal. Is the lua chat even designed to allow messages to be removed at all?

Any help would be appreciated.

I seemed to have put the wrong function for ChatScript.ChatMain.ChatChannel in the pastebin.

Here it is:

    function methods:RemoveMessage(messageData)
        self.MessageLogDisplay:RemoveMessage(messageData)
    end

I realised what’s causing it but I can’t seem to fix it.

You see, when I remove the message I only remove it from the MessageLogDisplay, but not MessageLog inside ChatChannel, this means the MessageLog will increase until it hits max and starts removing messages which is why the chat eventually disappears.

To fix this, I added a loop to the function in ChatChannel to also remove the message from the log.

        local int=0;
        for _, v in pairs(self.MessageLog) do
		int=int+1
		if(v.Message:gsub("%s+","")==messageData:gsub("%s+","")) then
			print("Successfully removed log.")
			table.remove(self.MessageLog,int)
		end
	end

I added some sanity check print statements before and after the loop to check the length of MessageLog, and it showed that I was indeed removing items from it (1 message log before loop, 0 after). But at the same time, this function contradicts those print statements:

function methods:AddMessageToChannel(messageData)
	table.insert(self.MessageLog, messageData)
	if self.Active then
		self.MessageLogDisplay:AddMessage(messageData)
	end
	if #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel then
		self:RemoveLastMessageFromChannel()
	end
	print("!!",dictionaryCount(self.MessageLog))
end

the print statement in this function continuously goes up as if I wasn’t removing items from the MessageLog. Why?

code formatting here is to surround your code snippet with ``` in front and behind.

alright, edited post with formatted code.

1 Like

Furthermore, it seems the issue is caused by the fact the channel “System” is also getting the messages as I found out by printing the messageData every time AddMessageToChannel was called. One message was from MGEngine (my channel) and one was from System. System was still filling up since I was only removing messages from MGEngine.

However, even if I:
remove messages from system as-well
completely stop system from being made a channel in the source code (proven by the fact that afterwards MGEngine was the only channel recieving system messages according to messageData)

the issue still persists.

Now, the way I CAN fix it, is just by doing

module.MessageHistoryLengthPerChannel = math.huge

inside ChatSettings but I’m not sure if this will affect performance in terms of memory usage? Obviously it won’t be a huge difference at first but if a server stays up for hours, these messages will still pile up.

Another solution would be completely nuking MessageLog, but due to my lack of knowledge on how the chat system works I’m not sure if that’ll have a butterfly effect and bork everything.