I’m currently working on a game where you will Roll a special item and it will then let everyone know in chat who rolled which items. Now I’ve made a quick snippet for this and It does work, however I’m having issues understanding how to color the text up. Documentation wasn’t helpful, neither were the countless premade discussions on here.
I’ve read about the RichTextMarkup or whatever it’s called, but I couldn’t seem to understand how it would work with a variable like ‘‘Message’’ in my case, instead of a premade string of text. Example:
local plr = game.Players.LocalPlayer
game:GetService('ReplicatedStorage'):WaitForChild('Chat').OnClientEvent:Connect(function(result)
print(result)
print(plr)
local Message = plr.Name..' Just Opened A: '..result..' Rarity!'
game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(Message)
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextChatService = game:GetService("TextChatService")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local OnMessageSent = RemoteEvents.OnMessageSent
OnMessageSent.OnClientEvent:Connect(function(Text, Metadata)
TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(Text, Metadata)
end)
TextChatService.OnIncomingMessage = function(TextChatMessage)
local overrideProperties = Instance.new("TextChatMessageProperties")
if TextChatMessage.Metadata == "RBXGeneral.SystemMessage" then
overrideProperties.Text = string.format("<font>[SERVER] %s</font>", TextChatMessage.Text)
elseif TextChatMessage.Metadata == "RBXGeneral.Player.Completed" then
overrideProperties.Text = string.format("<font color='#FFD700'>%s</font>", TextChatMessage.Text)
elseif TextChatMessage.Metadata == "RBXGeneral.Player.Died" then
overrideProperties.Text = string.format("<font color='#EC5800'>%s</font>", TextChatMessage.Text)
elseif TextChatMessage.Metadata == "RBXGeneral.MissingPermission" then
overrideProperties.Text = string.format("<font color='#9c9c9c'>%s</font>", TextChatMessage.Text)
end
return overrideProperties
end
But of course you will need to change the script. I have used different metadata’s to use different colors. For example RBXGeneral.Player.Died is used to make the color red when player died.
But for the basic example you can use the code below.
LocalScript - Main part that changes the color
local TextChatService = game:GetService("TextChatService")
TextChatService.OnIncomingMessage = function(TextChatMessage)
local overrideProperties = Instance.new("TextChatMessageProperties")
overrideProperties.Text = string.format("<font color='#FFD700'>%s</font>", TextChatMessage.Text)
return overrideProperties
end
Remember, that code is supposed to be written in a LocalScript
Thank you so much for providing this code. I’m having a hard time understanding how all this works and I can’t seem to make it work at the moment. Mainly because the code you provided (as far as I understand), only detects incoming messages, however I need this premade variable for a string sent out with a color instead of the orignial white boring one.
I’ll dig further into this and see If I can figure out what’s up and down and how I can implement it in my code snippet from above. Thank you!
I think you misunderstood. TextChatService.OnIncomingMessage event is fired when a message is sent to the channel. Also this events only purpose is to color or change the message.
So in this case you just need to send the message and a metadata.
ServerScript: Script - Send message every 5 seconds
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local Chat = ReplicatedStorage:WaitForChild("Chat")
local RarityTable = {
"Rare",
"Legendary",
"Common",
"Uncommon"
}
while task.wait(5) do
Chat:FireAllClients(RarityTable[math.random(#RarityTable)])
end
TextChatScript: LocalScript - Handle event and color the message
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local Chat = ReplicatedStorage:WaitForChild("Chat")
local RBXGeneral = TextChatService.TextChannels.RBXGeneral
Chat.OnClientEvent:Connect(function(Result)
local Message = Players.LocalPlayer.DisplayName .. " just opened a " .. Result .. " rarity!"
local Metadata = "RichTextMarkup"
RBXGeneral:DisplaySystemMessage(Message, Metadata)
end)
TextChatService.OnIncomingMessage = function(TextChatMessage)
if TextChatMessage.Metadata ~= "RichTextMarkup" then return end
local OverrideProperties = Instance.new("TextChatMessageProperties")
OverrideProperties.Text = string.format("<font color='#9c9c9c'>%s</font>", TextChatMessage.Text)
return OverrideProperties
end
If you also include an remote event called Chat in the ReplicatedStorage, then it will work just fine.