Is it possible to remove a message with player.Chatted?

i am making a game and im trying to make it so when someone uses an admin command it doesnt show up in chat im using player.Chatted and not TextChatService because i already have the chatted thing set up and i dont really understand how to use TextChatService

this is the code:

	player.Chatted:Connect(function(message)
		local Split = message:split(" ")
		local FiredCommand = Split[1]:split(Prefix)
		
		if Commands[FiredCommand[2]] then
			local Arguments = Split
			table.remove(Arguments, 1)
			
			if player.Admin.Value == true then
				table.insert(Admins, player.UserId)
			end
			
			if table.find(Admins, player.UserId) then
				if Arguments[3] then
					Commands[FiredCommand[2]](player, Arguments[2], Arguments[3])
				else
					Commands[FiredCommand[2]](player, Arguments[2])
				end
			end
		end
	end)
1 Like

You can’t remove the message with the .Chatted event. It’s a signal that fires when the player chats, there’s no way to remove the message through that.

However, TextChatService.OnIncomingMessage is a callback that needs to return a TextChatMessageProperties instance. You could use this to override the message, but you mentioned you don’t know how. Here’s an example:

--LocalScript

local function onIncomingMessage(message: TextChatMessage): TextChatMessageProperties
    --create properties
    local properties = Instance.new("TextChatMessageProperties")

    --set text to blank
    properties.PrefixText = " "
    properties.Text = " "

    --return the properties
    return properties
end

game:GetService("TextChatService").OnIncomingMessage = onIncomingMessage

Don’t forget to put in some sort of check for the player! You can get the player here:

--inside of the callback
local player = if message.TextSource then
    game:GetService("Players"):GetPlayerByUserId(message.TextSource.UserId)
    else nil
1 Like

thanks but how could i implement this into my code

Make a LocalScript in StarterPlayerScripts.

--services
local tcs = game:GetService("TextChatService")
local players = game:GetService("Players")

--your commands prefix
local prefix = "/"

--this callback will be invoked every time a message is sent
local function onMessage(message: TextChatMessage): TextChatMessageProperties
	--create basic properties
	local properties = Instance.new("TextChatMessageProperties")
	
	--first, check for the player
	local player = if message.TextSource then
		players:GetPlayerByUserId(message.TextSource.UserId) else nil
	
	--now, check for a command
	--%a+ represents any amount of consecutive characters
	local commandPart = string.match(message.Text, `{prefix}%a+`)
	
	--if both the player and the command part of the message exist, override the text
	if player and commandPart then
		properties.Text = " "
		properties.PrefixText = " "
	end
	
	--otherwise, the properties will be left as they are. You could put some more code in down here for things lile chat tags.
	
	
	--return the properties to be used
	return properties
end

--set the callback - this can only be done once
tcs.OnIncomingMessage = onMessage
1 Like

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