Admin command issue

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

why dont you use player.Chatted in a server script over this?

anyway i found the problem: the for loop
in conjunction with players.PlayerAdded, the onPlayerAdded function will run twice

1 Like

I believe player.Chatted is deprecated. It is replaced with TextChatService

1 Like

try this then

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
1 Like

It still runs twice.

(filler text for character limit)

Where was it stated to be deprecated? I went onto the DevHub and it does not state that it’s deprecated. Player (Chatted) | Documentation - Roblox Creator Hub

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.

Ig it’s not deprecated but is no longer supported by Roblox. Scripts using .Chatted do not work (I’m pretty sure)

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()
			...
1 Like

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

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