Fire the builtin Player.Chatted event from my script

I am trying to call the Player.Chatted function from my own custom chat script. It uses the serverscripts of the built in chat system and fires ReplicatedStorage.DefaultChatSystemChatEvents.SayMessageRequest, I get the messages to the other clients and into the chat system, and commands like /me work. However, whenever trying to pick this up from other scripts, Player.Chatted does not fire. After looking at the builtin chat script and the network traffic on all the chat system remote events, it seems like this is the only called RemoteEvent. Is Player.Chatted only available for corescripts?

Most likely scenario is that events like .Chatted are totally locked down and only fired if sent from a script with a higher permission level, which makes sense for moderation purposes and the like. You’re best just listening to events from the new Chat API rather than using .Chatted as I’d bet it’s using some legacy code to work or just is internally designed to only fire when messages are send from a corescript

1 Like

There are two ways to do that.

Way 1 - Chat Modules

There’s a module called ChatMain under PlayerScripts.ChatScript.ChatMain. When required, it returns a table with a custom event called MessagePosted, which you can :fire() with a message to fire Chatted.

local ChatMain = require(game:GetService("Players").LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("ChatScript"):WaitForChild("ChatMain"))
ChatMain.MessagePosted:fire("hello")

Way 2 - SetCore

StarterGui:SetCore() has an option called "CoreGuiChatConnections". It let’s you obtain useful chat events, including MessagePosted.

Unfortunately I don’t have an example for this one, as imo the documentation is unclear (doesn’t say if you have to use all events for example), and the example code too complex to understand at a first glance. You can however view it on this page (scroll to bottom)

5 Likes

Thanks! This worked perfectly!

Quite a bit late to the party here but could you explain this more to me? When I put it into my local script nothing happened

You also need to fire the SayMessageRequest remote event

ReplicatedStorage.DefaultChatSystemChatEvents.SayMessageRequest:FireServer("hello!","All")

to make it appear in the chat history

For reference, one of my implementations: tayChat/ChatInteraction.ts at 15c098fda36e2a550e728f00162649cab444818f · TayIorRobinson/tayChat · GitHub

1 Like

Old Chat System

Note that Player.Chatted is separate from the chat gui. The chat gui uses plain RemoteEvents to display messages. That’s why it might “seem” like nothing happens, but the actual Chatted event is still fired.

If you want to display a message in the chat gui you can use the solution above, or make use of the chat modules directly, like so:

local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
ChatService:GetSpeaker("PlayerName"):SayMessage("hello!", "All", {})

(Note that it won’t work if the player isn’t in game - you’ll need to create a new chat speaker in that case).
Sadly the documentation for that one died with the introduction of new docs, but it’s still available on the wayback machine. Though even that one wasn’t complete, so it’s best to view the very first one. You can also ask me questions about it since I know most ins and outs of it.

New Chat System

If your TextChatService.ChatVersion property is set to the new chat system, then all of the above is obsolete and you can both display a message in chat and fire Player.Chatted like this:

game:GetService("TextChatService").TextChannels.RBXGeneral:SendAsync("hello!")

However when it was first released, that code would not fire Chatted (it had some additional textbox focus checks I’m pretty sure), so it’s not fully reliable.