I’m creating an admin command that logs every chat message sent. The issue is that every time a player types something in chat, the TextChatService.OnIncomingMessage = function(message: TextChatMessage) runs two times, instead of once. So I end up with two slots displaying the same message, instead of just one. Why is this running twice per chat message?
LocalScript:
local replicatedStorage = game:GetService("ReplicatedStorage")
local slot = replicatedStorage:WaitForChild("ChatSlot")
local players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local function onPlayerAdded(player)
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
local properties = Instance.new("TextChatMessageProperties")
if message.TextSource then
local newSlot = slot:Clone()
newSlot.Parent = script.Parent
newSlot.Name = "Slot"
newSlot.Log.Text = tostring(message.Text)
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
playerList = players:GetChildren()
for i=1, #playerList do
onPlayerAdded(playerList[i])
end
local replicatedStorage = game:GetService("ReplicatedStorage")
local slot = replicatedStorage:WaitForChild("ChatSlot")
local players = game:GetService("Players")
local firstPlayers = players:GetPlayers()
local TextChatService = game:GetService("TextChatService")
function onPlayerAdded(player)
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
if message.TextSource and message.TextSource == player then
local newSlot = slot:Clone()
newSlot.Parent = script.Parent
newSlot.Name = "Slot"
newSlot.Log.Text = tostring(message.Text)
end
end
end
players.PlayerAdded:Connect(function(player)
if not table.find(firstPlayers, player) then onPlayerAdded(player)
end)
for _, v in firstPlayers do
onPlayerAdded(v)
end
firstPlayers = nil
If that’s true, they need to update the documentation to reflect this change. I doubt it though considering I created something using Player.Chatted years ago and it still worked about a week or two ago.
Anyways, to the issue at hand- you could create a temporary cooldown until it accepts a new message. For example-
local cooldowns = {}
local function onPlayerAdded(player)
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
if cooldowns[player.Name] == true then
return
end
cooldowns[player.Name] = true
delay(1/30, function()
cooldowns[player.Name] = nil
end)
local properties = Instance.new("TextChatMessageProperties")
if message.TextSource then
local newSlot = slot:Clone()
...
Nevermind guys, I found the solution. I changed the function to this: TextChatService.MessageReceived:Connect(function(TextChatMessage) and it works fine now. Thanks for the help