Help on using TextChatService to detect if a user has said a specific message?

I’m trying to make a tool that detects if the player holding it has said a specific message and then does a specific thing based on what was said. I know Harry Potter themed magic games do this, I’m just lost as to how.

I can’t make heads or tails of the documentation so I’m looking for a little more simplified or concise explanation on the process and what methods are necessary for this to work.

1 Like

In order to accomplish this, you’ll need to use TextChannel’s OnIncomingMessage callback.

TextChatService.OnIncomingMessage is called when a message is sent in chat. Y

You’d then need to check if the message sent by the player matches the string of your choosing.

Sample script:
(The callback can only be defined on the client, so ensure this is a local script!)

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

TextChatService.OnIncomingMessage = function(message: TextChatMessage)


	if message.TextSource then
		if message == "hello friends" then
           print("hello")
        end
	end

end

The provided script didn’t seem to work, is there a missing variable or some other code I’m supposed to add in before this works? Or did I put it in the wrong area? I made a LocalScript in StarterPlayerScripts and pasted it in to test.

Try replacing

if message == "hello friends" then

with

if message.Text == "hello friends" then

message itself isn’t a string, so you use .Text

2 Likes

To achieve this you need to use roblox’s player.Chatted event document.

This allows you to control the player’s messages and depending on if they typed a message that matches your command, you can manipulate what to do.

Sample Script:

game.Players.PlayerAdded:Connect(function(player)
	plr.Chatted:Connect(function(msg)
        if msg == "respawn" then
            player:LoadCharacter()
        end
    end)
end)
1 Like

This worked, but print(“hello”) is executing twice. I only need to it to execute once, why is this occurring?

because it replicates to other clients too

I think you should juse use player.Chatted because its universal and works for both Legacy and TextChatService.

Apologies - this happens because OnIncomingMessage fires twice, first from being sent initially and received by the client and the second from being received by the same client from the server or atleast that’s what i got from the documentation, correct me if im wrong :stuck_out_tongue:

You can counteract this by adding another condition to any of the if statements, being

message.Status == Enum.TextChatMessageStatus.Success

which checks if the message’s Status is Success (which means it’s been received by the client from the server), so it’d look something like this;

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

TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	if message.TextSource then
		if message.Text == "hello friends" and message.Status == Enum.TextChatMessageStatus.Success then
           print("hello")
        end
	end
end