Need help displaying display names in TextChatService

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

    I need to display all players display names in chat

  2. What is the issue?

    This feature is integrated already as a default for games, but for this one somehow it got disabled and for the life of me i cannot figure out how to fix it. It shows the player’s actual username in chat, but i need the display name.

  3. What solutions have you tried so far?

    After searching the devforum i see stuff on how to disable displaynames, but not how to enable them (yes i’ve tried to reverse this process to no avail). There is fix i can see that will help me re-enable these display names that used to be default in the new TextChatService as shown here.

I tried to use this code, and it works except for the part where it doesn’t display the client it runs on’s display name, just everyone elses.

I tried this code already to fix it, maybe i was on the right track??

-- this is on the client
local PlayersService = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")

local localPlayer = game.Players.LocalPlayer

local function onMessageReceived(textChatMessage: TextChatMessage)
	local textSource = textChatMessage.TextSource
	local chattingPlayer = if textSource
		then PlayersService:GetPlayerByUserId(textSource.UserId)
		else nil

	if chattingPlayer then
		textChatMessage.PrefixText = (textChatMessage.PrefixText:gsub(chattingPlayer.Name, chattingPlayer.DisplayName))
	end
end
local function onSendingMessage(textChatMessage: TextChatMessage)
	textChatMessage.PrefixText = (textChatMessage.PrefixText:gsub(localPlayer.Name, localPlayer.DisplayName))
end

-- connect to events
TextChatService.MessageReceived:Connect(onMessageReceived)
TextChatService.SendingMessage:Connect(onSendingMessage)

Thank you for in advance! :upside_down_face:

Do you have any other code that interfaces with MessageReceived and SendingMessage?
Seems like some other script such as the ChatColours could be changing the prefix

this is that script

--!strict

----------------- ╭──────────╮ -----------------
----------------- │ Services │ -----------------
----------------- ╰──────────╯ -----------------
local PLAYERS           = game:GetService("Players")
local TEXT_CHAT_SERVICE = game:GetService("TextChatService")



--============================================--

---------------- ╭───────────╮ -----------------
---------------- │ Functions │ -----------------
---------------- ╰───────────╯ -----------------
function OnIncomingMsg(msg: TextChatMessage)
    local player = PLAYERS:GetPlayerByUserId(msg.TextSource.UserId)
    local minutes = player.leaderstats.Minutes.Value
    local properties = Instance.new("TextChatMessageProperties")
    properties.PrefixText = 
        if minutes < 5000 
        then `<font color="#FFF"><b>{msg.TextSource.Name}:</b></font>`
        else `<font color="rgb(247, 38, 59)"><b>{msg.TextSource.Name}:</b></font>`
    return properties
end

-------------- ╭────────────────╮ --------------
-------------- │ Initialisation │ --------------
-------------- ╰────────────────╯ --------------
TEXT_CHAT_SERVICE.OnIncomingMessage = OnIncomingMsg

This script throws an error about textsource being nil. Disabling it fixes chat display names. I also cannot figure out what is wrong with this one, this was done by someone else. Any help would be appreciated! thanks for pointing that out to me!

You need to get the user’s display name from player.DisplayName, not msg.TextSource.Name

--!strict

----------------- ╭──────────╮ -----------------
----------------- │ Services │ -----------------
----------------- ╰──────────╯ -----------------
local PLAYERS           = game:GetService("Players")
local TEXT_CHAT_SERVICE = game:GetService("TextChatService")



--============================================--

---------------- ╭───────────╮ -----------------
---------------- │ Functions │ -----------------
---------------- ╰───────────╯ -----------------
function OnIncomingMsg(msg: TextChatMessage)
	local player = PLAYERS:GetPlayerByUserId(msg.TextSource.UserId)
	local minutes = player.leaderstats.Minutes.Value
	local properties = Instance.new("TextChatMessageProperties")
	properties.PrefixText = 
		if minutes < 5000 
		then `<font color="#FFF"><b>{player.DisplayName}:</b></font>`
		else `<font color="rgb(247, 38, 59)"><b>{player.DisplayName}:</b></font>`
	return properties
end

-------------- ╭────────────────╮ --------------
-------------- │ Initialisation │ --------------
-------------- ╰────────────────╯ --------------
TEXT_CHAT_SERVICE.OnIncomingMessage = OnIncomingMsg

And for the nil error, do you have any sort of custom system messages in-game?

i do not! its just the random color stuff this guy did, it was done without my knowledge. and the client display name script which basically works except for the local player
image

thank you so much! i completely missed this :sob: and figured it out myself at the same time. Literally dk how i missed it. Thanks again :upside_down_face:

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