TextChatService Help!

Just like in the title, I am using the TextChatService for my chat necessities.

I am using the proximity-chat script provided by roblox

https://devforum.roblox.com/t/textchatservice-new-chat-window-customization-and-delivery-apis/2054343

^In that post,

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local generalChannel : TextChannel = TextChatService:WaitForChild("TextChannels").RBXGeneral

local function getPositionFromUserId(userId)
	local targetPlayer = Players:GetPlayerByUserId(userId)
	if targetPlayer then
		local targetCharacter = targetPlayer.Character
		if targetCharacter then
			return targetCharacter:GetPivot().Position
		end
	end

	-- return default position for example
	return Vector3.new(0, 0, 0)
end

generalChannel.ShouldDeliverCallback = function(textChatMessage: TextChatMessage, targetTextSource: TextSource)
	local sourcePos = getPositionFromUserId(textChatMessage.TextSource.UserId)
	local targetPos = getPositionFromUserId(targetTextSource.UserId)

	-- If the distance between players is less than 50, deliver the message.
	return (targetPos - sourcePos).magnitude < 50
end

Here is the server script, is it parented to server script service.

My question about this is, while the script works perfectly fine, as seen is this screenshot

  1. How would I make it so the player names are changed, instead of it being their user names, it’ll be their character names they use based on my custom character creator

image

Talking about those names, how would I change that.
2. Secondly, I want to make multiple commands, such as /shout [Makes the players message yellow, and the proximity distance will be higher], /me [A roleplay action, the distance will be the same as the regular talking], and others as well. How would I do something like this.
A solution I came up with is…

  • 1: Making multiple scripts, and each script is for each command, but I feel as if this would be way too inefficient.

Are there any other solutions? IS there anybody that can help me?

image

These would also be the commands, how would I implement them in the script, make them each different colors when messaged, and also proximity based as well?

1 Like

To replace a player’s username with their character’s name, you can do something like this in a LocalScript:

TextChatService.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)
		
		-- This is assuming the name is stored in a stringValue in the player object. Change this to whatever you need.
		local playerCharacterName = player.CharacterName.Value
		
		-- Default is message.PrefixText and will just show the player's username.
		properties.PrefixText = playerCharacterName
	end

	return properties
end

Refer to this article for more information.

Thanks, I tweaked it a little and also added a text color.

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


TextChatService.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)

		-- This is assuming the name is stored in a stringValue in the player object. Change this to whatever you need.
		local playerCharacterName = string.format("<font color='#%s'>%s</font>", Color3.fromRGB(255,255,255):ToHex(), player.PlayerStatsFolder.FirstName.Value .. " " .. player.PlayerStatsFolder.LastName.Value .. " says:")

		-- Default is message.PrefixText and will just show the player's username.
		properties.PrefixText = playerCharacterName
	end

	return properties
end

This is a local script in starterplayerscripts.
My proximity script (Shown in earlier posts) is in serverscriptservice as a serverscript.

My question is, how can I make it so the commands make the proximity distance more or less, and also change the text color? depending on what command was used?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.