Hiding chats from a textchatservice channel

Hey there

I am having issues deleting messages that are not recieved from the server in the chat channel “Logs”, do not edit the staff one though.

Current code I have:

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local logWelcomeEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("Other"):WaitForChild("LogEvent")
local staffWelcomeEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("Other"):WaitForChild("StaffEvent")

local function createStaffChannel()
	local staffChannel = Instance.new("TextChannel")
	staffChannel.Name = "Staff"
	staffChannel.Parent = TextChatService:WaitForChild("TextChannels")
end

local function createLogsChannel()
	local logsChannel = Instance.new("TextChannel")
	logsChannel.Name = "Logs"
	logsChannel.Parent = TextChatService:WaitForChild("TextChannels")

	logsChannel.MessageReceived:Connect(function(message)
		local source = message.TextSource
		if source then
			print("Message received from:", source.Name)
			if source:IsA("Player") then
				print("Deleting message from player:", source.Name)
				message:Destroy()
			else
				print("Message is not from a player:", source.Name)
			end
		else
			print("Message source is nil")
		end
	end)
end

local function addPlayerToStaffChannel(player)
	if player:GetRankInGroup(8423759) >= 6 then
		local staffChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("Staff")
		if staffChannel then
			staffChannel:AddUserAsync(player.UserId)
			staffWelcomeEvent:FireClient(player, "Hello %s! Only staff can see and type in this channel.")
		end
	end
end

local function addPlayerToLogsChannel(player)
	if player:GetRankInGroup(8423759) >= 6 then
		local logsChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("Logs")
		if logsChannel then
			local textSource = logsChannel:AddUserAsync(player.UserId)
			textSource.CanSend = false
			logWelcomeEvent:FireClient(player, "Hello %s, in this channel (logs) you are not allowed to send messages. These are for flagging events and other actions to assist in moderation.")
		end
	end
end

createStaffChannel()
createLogsChannel()

Players.PlayerAdded:Connect(function(player)
	addPlayerToStaffChannel(player)
	addPlayerToLogsChannel(player)
end)

for _, player in Players:GetPlayers() do
	addPlayerToStaffChannel(player)
	addPlayerToLogsChannel(player)
end

(by the way, yes I have debugged it and nothing has printed.)

The text channel “Logs” does get created. Not sure why it won’t work.

Thanks :heart:

1 Like

Still needing help.

This is pretty annoying since it interferes with our moderation logs.

1 Like

You need to use OnIncomingMessage

1 Like

Got it, trying it quickly

1 Like

Tried it:

TextChatService:WaitForChild("TextChannels"):WaitForChild("Logs").OnIncomingMessage = function(message)
	local source = message.TextSource
	if source then
		if source:IsA("Player") then
			message:Destroy()
		end
	end
end

Nothing happened.

Did I get the textsource wrong?

1 Like

Read. The. Documentation. Please.

1 Like

I did. The documentation isn’t going to help me if something is going wrong.

At least provide some code I could use.

1 Like

You are not supposed to destroy anything.

“Can only be implemented on the client.”

“Called when TextChannel is receiving an incoming message”

“If this callback returns a TextChatMessageProperties, those properties are merged with the TextChatMessage parameter to create a new TextChatMessage.”

Don’t return anything instead so the incoming message is canceled

1 Like

Thanks for the information I didn’t already know.

I said provide some code. If you aren’t going to do that, please stop helping me.

1 Like

I have a new script now from trialing a little bit.

local logsChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("Logs")

logsChannel.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	properties.Text = message.Text

	if message.TextSource then
		local sourcePlayer = Players:GetPlayerByUserId(message.TextSource.UserId)
		if sourcePlayer then
			return nil
		end
	end

	return properties
end
1 Like

I just read through the documentation (that the other guy sent) and TextSource is the user’s name and information on things like their permission to the channel, etc.

TextSource is the data that represents a speaker in a TextChannel.

I am attempting to figure it out, please have patience.

1 Like

I’m not sure you can have a TextSource WITHOUT a user.

You can’t do “if source:IsA(“Player”)” because TextSource ISN’T a player, it’s a TextSource.

The TextSource’s name is going to be the DisplayName of whatever player it is for, the TextSource has this information from TextSource.UserID.

That was my old code:

I changed some stuff around.

Is that code integrated with the rest of the code in the initial post?

No. This is client sided now. The one before has had this part taken out:

And put in a client sided script:

local logsChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("Logs")

logsChannel.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	properties.Text = message.Text

	if message.TextSource then
		local sourcePlayer = Players:GetPlayerByUserId(message.TextSource.UserId)
		if sourcePlayer then
			return nil
		end
	end

	return properties
end

Can you explain the issue again? Or if it has changed at all, what is it now?

Basically, there is no errors, but it won’t delete any messages incoming from players on the Logs text channel.

Where is it trying to remove messages in the new code?

Honestly, I’m more experienced with legacy chat service, that’s why I’m messed up here.

Is the “return nil” supposed to delete the message??

And I JUST started messing around with and learning TextChatService (or any chat in general) the other day.