Message Doubled after being Filtered

Greetings dear reader,

I am currently working on some sort of a police roleplay pager. In this pager regular messages are just sown once, and the frame, that I put as a template, is not getting cloned twice.

However when I type something in chat, that is filtered by Roblox’s TextService, then this message appears twice on the pager. So for some reason one of the following functions is ran twice, instead of once causing it to appear twice.

On a side-note I have also sent messages just containing tags (###) in it. The message appeared only once. This means only tagged messages appear twice.

Down below I will provide some snippets of this script, which are responsible for cloning and filtering the message.

Local Script

local TextChatService = game:GetService("TextChatService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:FindFirstChild("SendChatMessage")
local TextChatService = game:GetService("TextChatService")

local lastSentMessage = nil

TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	if Player.Team.Name == "Autobahnpolizei" or "Feuerwehr" or "Polizei" or "Rettungsdienst" or "SEK" or "THW" or "Zoll" then
		if message.TextSource then
			local player = game.Players:GetPlayerByUserId(message.TextSource.UserId)
			if player == game.Players.LocalPlayer then
				if MicValue == true then
					if message.Text ~= lastSentMessage then
						lastSentMessage = message.Text
						MessageEvent:FireServer(AssignedChannel, message.Text)
						print("Message Sent to server")
					end
				else
					return;
				end
			end
		end
	end
end

MessageEvent.OnClientEvent:Connect(function(Plr, Channel, FilteredMessage, ImageInfo)
	if Player.Team.Name == "Autobahnpolizei" or "Feuerwehr" or "Polizei" or "Rettungsdienst" or "SEK" or "THW" or "Zoll" then
		print("Message received by client")
		local PlrName = Plr.Name

		if Channel == AssignedChannel then
			if lastSentMessage == FilteredMessage then
				local Clone = Frame:Clone()

				Clone.ImageLabel.Image = ImageInfo
				Clone.Message.Text = PlrName..": "..FilteredMessage
				LayoutOrder(Clone)
				Clone.Parent = Display.MessageFrame
				Display.NotificationSound:Play()

				if Channel == "Polizei" then
					table.insert(PolizeiHistory, tostring(PlrName.."SPLIT"..FilteredMessage.."SPLIT"..ImageInfo))

					if #PolizeiHistory == 8 then
						table.remove(PolizeiHistory, 1)
					end
				elseif Channel == "Feuerwehr" then
					table.insert(FeuerwehrHistory, tostring(PlrName.."SPLIT"..FilteredMessage.."SPLIT"..ImageInfo))

					if #FeuerwehrHistory == 8 then
						table.remove(FeuerwehrHistory, 1)
					end
				elseif Channel == "Rettungsdienst" then
					table.insert(RettungsdienstHistory, tostring(PlrName.."SPLIT"..FilteredMessage.."SPLIT"..ImageInfo))

					if #RettungsdienstHistory == 8 then
						table.remove(RettungsdienstHistory, 1)
					end
				elseif Channel == "Team" then
					table.insert(TeamHistory, tostring(PlrName.."SPLIT"..FilteredMessage.."SPLIT"..ImageInfo))

					if #TeamHistory == 8 then
						table.remove(TeamHistory, 1)
					end
				end
			end
		end
	end
end)

Server Script

local function FilterMessage(MessageToFilter, UserID)
	return TextService:FilterStringAsync(MessageToFilter, UserID, Enum.TextFilterContext.PublicChat):GetChatForUserAsync(UserID)
end

MessageEvent.OnServerEvent:Connect(function(Player, Channel, Message)
	print("Message received by server")    
	local Badge = nil
	
	print(Player.Team)

	Badge = Badges[Player.Team.Name]

	local filteredMessage = FilterMessage(Message, Player.UserId)

	print(Badge)
	
	MessageEvent:FireAllClients(Player, Channel, filteredMessage, Badge)
end)

Any help would be greatly appreciated.

Thank you very much,
tr2vl

1 Like

A temporary fix would be to take the doubled message and only keep a half of it using the string library. But we gotta fix what we gotta fix. Try printing the parameters inside the FilterMessage function

2 Likes

I have looked at this post Admin command issue and figured it was the exact same issue. So this was an issue related to me using TextChatService.OnIncomingMessage instead of TextChatService.MessageReceived.

So if you want to know what exactly I have changed look here:

TextChatService.MessageReceived:Connect(function(message)
	if Player.Team.Name == "Autobahnpolizei" or "Feuerwehr" or "Polizei" or "Rettungsdienst" or "SEK" or "THW" or "Zoll" then
		if message.TextSource then
			local player = game.Players:GetPlayerByUserId(message.TextSource.UserId)
			if player == game.Players.LocalPlayer then
				if MicValue == true then
					if message.Text ~= lastSentMessage or game:GetService("Chat"):FilterStringForBroadcast(message.Text, Player):find("#") then
						lastSentMessage = message.Text
						MessageEvent:FireServer(AssignedChannel, message.Text)
						print("Message Sent to server")
					end
				else
					return;
				end
			end
		end
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.