Make script post message in default chat?


#1

I feel like I have at some point seen the ability to have a script push a message to the chat window and you could choose the color of the text and everything.
The problem is I can’t for the life of me figure out how I did it and I want to be able to add chat commands and that means feedback in the chat itself.
Does anyone know how a script can push a message to the chat? thanks.


#2

Check out the SetCore method of StarterGui. You can use ChatMakeSystemMessage to write custom messages to the chat.


#3

StarterGui:SetCore() is your friend here
However you can only run it in LocalScripts

An example being; lets say you wanted to post the message ‘Hello world’ in SourceSans and a cyan-ish colour to every client

Using a RemoteEvent I will call SendMessage we can do something like this

On a server-sided script

SendMessage:FireAllClients({  --FireAllClients will send it to every player in the players server, you can also use SendMessage:FireClient(player Target, chatProperties)
      Text = “Hello World!”; --The message to be printed (Required)
      Color = Color3.fromRGB(0,255,215); --The colour of the output text (Optional, will default to a white-ish colour)
      Font = Enum.Font.SourceSans; --The font of the message (Optional, will default to SourceSans)
      FontSize = Enum.FontSize.Size18; --The size of the message (Optional, will defualt to Size18)
   })

And in a client script

SendMessage.OnClientEvent:Connect(function(chatProperties) --Recieves the event sent from the player (excluding the player argument if we used :FireClient()
game:GetService(“StarterGui”):SetCore(“ChatMakeSystemMessage”,chatProperties) --Outputs the message to the client’s chat window
end

You only need the Text = string part as the rest defaults if not set

ps this is partially a fe tutorial but ehh


#4

There are two ways I can think of off the top of my head on how you can achieve this behavior. The first method is the one you probably remember, which uses SetCore. The second method is possible using features introduced in Roblox’s Lua chat.


Method 1: SetCore

SetCore is a method under StarterGui that gives you the ability to perform actions with Roblox’s core scripts. Note that SetCore only works in local scripts. SetCore is not guaranteed to be registered when your client starts up, so make sure all SetCore calls are in a function wrapped in a pcall to prevent errors.

For this case, we are specifically interested in ChatMakeSystemMessage. ChatMakeSystemMessage gives you the power to send messages to the chat box, with control over the text, text color, font, and text size.

To use ChatMakeSystemMessage, you first need to get the StarterGui service. Once you have a variable referencing StarterGui, run a function on repeat until it runs successfully. To check for this, you use the pcall mentioned earlier. The first returned value of a pcall is a boolean. If it’s true, that means the function ran successfully. Inside the function, we use SetCore to make the chat message, which I will explain in comments in the example code.

Example code:

--Get the StarterGui service
local StarterGui = game:GetService("StarterGui")

--Use a repeat to make sure we get our system message
--The repeat will only stop once the function successfully runs
repeat
	wait() --Wait to prevent crashing
	local Success = pcall(function()
		--Run SetCore method inside the pcall
		--The first argument of SetCore is the method we wish to use
		--In this case, the second argument is a dictionary of data for the chat message
		StarterGui:SetCore("ChatMakeSystemMessage", {
			Text = "This is a system message"; --The chat message
			Color = Color3.fromRGB(0, 255, 255); --Chat message color, defaults to white
			Font = Enum.Font.SourceSansBold; --Chat message font, defaults to SourceSansBold
			TextSize = 18 --Text size, defaults to 18
		})
	end)
until Success

Method 2: Lua Chat Speaker

Roblox’s Lua chat gives developers the ability to make speakers, which can send messages to different channels. Before we go through how to program a speaker to send messages, it’s good to get an understanding of the Lua chat’s structure, which you can see here:

Chat Structure

The Roblox chat system is built around three components: Speakers, Messages, and Channels.

A Speaker is an entity that can speak in chat. All players who join the game are automatically a speaker. Speakers can also be bots that are created and managed by code.

A Message is a container for content that a speaker enters into their chat. This can be text to send to other speakers or a command that performs a specific action in-game. Messages can also contain metadata which can be used to format the message or add extra functionality to commands.

A Channel is a section of chat where only certain Speakers can see and write messages. By default each player is automatically added to the All and System channels, although they will also be added to a Team channel if they are assigned a team. Whispers between players also use a channel per each pair of participants.

Source: https://wiki.roblox.com/index.php?title=Lua_Chat_System

Now that we have a better understanding of Roblox’s Lua chat, we can make a speaker which can chat for us. There are a few API pages of interest for doing this, listed here:

ChatService Server API: https://wiki.roblox.com/index.php?title=Lua_Chat_System/Server_API/ChatService
ChatChannel Server API: https://wiki.roblox.com/index.php?title=Lua_Chat_System/Server_API/ChatChannel *not used in example
ChatSpeaker Server API: https://wiki.roblox.com/index.php?title=Lua_Chat_System/Server_API/ChatSpeaker

Example

The first thing you want to do is require the ChatService module in a server script. The path to this module is game > ServerScriptService > ChatServiceRunner > ChatService. It can be obtained using the following code:

local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

Before making our chat speaker, it’s important to make sure the speaker has a chat channel to join. The channel we will be waiting for is the default ‘All’ channel. We can make sure it exists with ChatService:GetChannel("All"). If the channel does not exist, we need to wait for it. To do so, we need to use a while loop, and wait for new channels using the ChannelAdded event of ChatService. By waiting for this event to run, we can get the name of any channels created. Once the ‘All’ channel is created, we can break (reference: http://robloxdev.com/articles/Roblox-Coding-Basics-Loops) the loop.

if not ChatService:GetChannel("All") then
	while true do
		local ChannelName = ChatService.ChannelAdded:Wait()
		if ChannelName == "All" then
			break
		end
	end
end

Now that we know the ‘All’ channel exists, we can create our speaker and have it join the ‘All’ channel. To do so, we use the AddSpeaker method of ChatService. Once we have our speaker, we use the JoinChannel method of the speaker to make it join the ‘All’ channel.

local MrSpeaksEveryMinute = ChatService:AddSpeaker("Mr. Speaks Every Minute")
MrSpeaksEveryMinute:JoinChannel("All")

With our chat speaker ready, we are finally able to make it speak. However, before we make it speak, we want to customize the speaker’s extra data to change certain properties of its chat messages. To do so, we use the SetExtraData method of our speaker. Here is a table of extra data you can set:

Key Data Type
NameColor Color3
ChatColor Color3
Font Enum.Font
TextSize int
Tags array

Most of these should be self explanatory in a moment, however the ‘Tags’ extra data requires some explanation. The array used for the ‘Tags’ extra data consists of any number of dictionaries inside. Each dictionary requires two pieces of data, which can be seen in this table:

Index Data Type
TagText string
TagColor Color3

Tag dictionary example

local VipTag = {TagText = "VIP"; TagColor = Color3.fromRGB(255, 0, 0)}
local CyanTag = {TagText = "i'm really just an example"; TagColor = Color3.fromRGB(0, 255, 255)
Speaker:SetExtraData("Tags", {VipTag, CyanTag})

With an understanding of extra data, we can easily customize our speaker, as seen here:

MrSpeaksEveryMinute:SetExtraData("NameColor", Color3.fromRGB(255, 0, 191))
MrSpeaksEveryMinute:SetExtraData("ChatColor", Color3.fromRGB(255, 255, 204))

Now that we have finished setting everything up, it’s finally time to start making our speaker talk. To do so, we just call the SayMessage method of our speaker. Since this is Mr. Speaks Every Minute, we are going to run it in a loop in this example, as seen here:

while true do
	MrSpeaksEveryMinute:SayMessage("I speak every minute!", "All")
	wait(60) --Wait 60 seconds
end

Full code example:

--Get ChatService
local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

--Wait for the channel 'All' to exist
if not ChatService:GetChannel("All") then
	while true do
		local ChannelName = ChatService.ChannelAdded:Wait()
		if ChannelName == "All" then
			break
		end
	end
end

--Make your speaker and have it join the 'All' channel we waited for
local MrSpeaksEveryMinute = ChatService:AddSpeaker("Mr. Speaks Every Minute")
MrSpeaksEveryMinute:JoinChannel("All")

--Change data for your speaker such as name color, chat color, font, text size, and tags
MrSpeaksEveryMinute:SetExtraData("NameColor", Color3.fromRGB(255, 0, 191))
MrSpeaksEveryMinute:SetExtraData("ChatColor", Color3.fromRGB(255, 255, 204))

--Run a loop to chat in intervals
while true do
	MrSpeaksEveryMinute:SayMessage("I speak every minute!", "All")
	wait(60) --Wait 60 seconds
end

Ending Notes

There are two methods of interest I didn’t include examples for. I was unable to get either of them working, and one of them seems to have no customization options. Anyway, here they are below:

https://wiki.roblox.com/index.php?title=Lua_Chat_System/Server_API/ChatChannel#SendSystemMessage
https://wiki.roblox.com/index.php?title=Lua_Chat_System/Server_API/ChatSpeaker#SendSystemMessage


#5

Thank you for such an in depth answer that covers how to do it in the Lua chat