I have this thing that adds a “Speaker” to the game (called “System”) through the Chat service, and it can post things in chat using :SayMessage(). That part works just fine. However, how would I make it PM a single player instead of the “All” channel? Is it even possible?
SSS = game:GetService("ServerScriptService")
ChatServ = require(SSS:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
if not ChatServ:GetChannel("All") then
while true do
local ChannelName = ChatServ.ChannelAdded:Wait()
if ChannelName == "All" then
break
end
end
end
local System = ChatServ:AddSpeaker("System")
System:JoinChannel("All")
System:SetExtraData("NameColor", Color3.fromRGB(255, 0, 0))
System:SetExtraData("ChatColor", Color3.fromRGB(255, 0, 0))
System:SayMessage("test", "All")
The PrivateMessaging module (used in the /w command) will create a different ChatChannel with only the two users who are chatting. You can look more into this at the game.CoreGui.RobloxGui.Modules.Server.ServerChat.DefaultChatModules.PrivateMessaging module. Alternatively, you can just get the ChatSpeaker of the player you want to send a message to, and use the :SendSystemMessage method on the ChatSpeaker to create a message that will only show for them.
I’m having trouble getting the chat speaker of a given player. Do you know how I would go about doing that?
EDIT: And to follow up, how might I get the player that owns a speaker after I use the SpeakerAdded event? Would it be the speaker’s name?
To get the chat speaker of a player do ChatService:GetSpeaker(plr.Name)
To get the player of a chat speaker do ChatSpeaker:GetPlayer() (returns nil if the chat speaker is a bot)
I think the ExtraData argument is optional though. If you aren’t using the “System” speaker for anything else except sending private messages, there’s really no use for it.
(Edit: You could also set the channel to “System” so that the message appears with {System} before it, like how the friend join notification works)