TextChatService problem

I have problem when the player not in radius then its send this warn:
and when i do Properties.Text = nil its sends me a error: Unable to assign property Text. string expected, got nil

				if string.lower(string.sub(Message.Text, 1, 3)) == "/me" then
					local magnitude = (Player.Character:FindFirstChild("Head").Position - LocalPlayer.Character:FindFirstChild("Head").Position).magnitude
					if magnitude < config.Radius then
						Properties.Text ="<font color='#"..config.meColor.."'> "..string.sub(Message.Text, 5).."</font> "
					else
						Properties = {}
					end
					return Properties
				end

1 Like

Can you send the whole script, not just that?

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

local LocalPlayer = Players.LocalPlayer

local config = require(script.Config)

TextChatService.OnIncomingMessage = function(Message: TextChatMessage)
    local Properties = Instance.new("TextChatMessageProperties")
    
    if Message.TextSource then
        local Player = Players:GetPlayerByUserId(Message.TextSource.UserId)
        if Player then
            if Player.Character and Player.Character:FindFirstChild("Head") and LocalPlayer.Character then
                if string.lower(string.sub(Message.Text, 1, 3)) == "/me" then
                    local magnitude = (Player.Character:FindFirstChild("Head").Position - LocalPlayer.Character:FindFirstChild("Head").Position).magnitude
                    if magnitude < config.Radius then
                        Properties.Text = "<font color='#"..config.meColor.."'> "..string.sub(Message.Text, 5).."</font> "
                    else
                        Properties = {}
                    end
                    return Properties
                end
            end
        end
    end
    
    return Properties
end

You have to return a TextChatMessageProperties instance, not an empty array. If you do not want text, just set it to an empty string: Text = ""

3 Likes

Will this work? Tell me any errors if it fails.

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

local LocalPlayer = Players.LocalPlayer

local config = require(script.Config)

TextChatService.OnIncomingMessage = function(Message: TextChatMessage)
    local Properties = Instance.new("TextChatMessageProperties")
    
    if Message.TextSource then
        local Player = Players:GetPlayerByUserId(Message.TextSource.UserId)
        if Player then
            if Player.Character and Player.Character:FindFirstChild("Head") and LocalPlayer.Character then
                if string.lower(string.sub(Message.Text, 1, 3)) == "/me" then
                    local magnitude = (Player.Character:FindFirstChild("Head").Position - LocalPlayer.Character:FindFirstChild("Head").Position).magnitude
                    if magnitude < config.Radius then
                        Properties.Text = "<font color='#"..config.meColor.."'> "..string.sub(Message.Text, 5).."</font>"
                    else
                        Properties.Text = ""
                    end
                    return Properties
                end
            end
        end
    end
    
    return Properties
end

(if it works, please click the solution button on this post. thanks :slight_smile: )

now its like that:

Player who chatted:

Player not in radius:

There is no error, but how can I make it so that players not in the radius do not see the message?

Is this being handled on the client or server side?

client in StarterPlayerScripts

Okay give me 1 second, I’ll give you a tutorial on how to fix this.

Since you are handling this on the client, it’s not updating on the server.

1 Like

Okay so, by default this is going to create a remote event for you (but you can edit this if you want)

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

local ChatMessageEvent = Instance.new("RemoteEvent")
ChatMessageEvent.Name = "ChatMessageEvent"
ChatMessageEvent.Parent = ReplicatedStorage

local config = require(script.Config)

Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
            local senderPosition = player.Character.HumanoidRootPart.Position
            
            for _, recipient in ipairs(Players:GetPlayers()) do
                if recipient.Character and recipient.Character:FindFirstChild("HumanoidRootPart") then
                    local recipientPosition = recipient.Character.HumanoidRootPart.Position
                    local distance = (recipientPosition - senderPosition).Magnitude
                    
                    if distance <= config.Radius then
                        ChatMessageEvent:FireClient(recipient, player.Name, message)
                    else
                        ChatMessageEvent:FireClient(recipient, player.Name, "")
                    end
                end
            end
        end
    end)
end)

This will handle the range of the sender, and then each recipient will be checked if they are in the range. This is where the client script kicks in.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChatMessageEvent = ReplicatedStorage:WaitForChild("ChatMessageEvent")

ChatMessageEvent.OnClientEvent:Connect(function(senderName, message)
    local TextChatService = game:GetService("TextChatService")

    if message == "" then
        local Properties = Instance.new("TextChatMessageProperties")
        Properties.Text = ""
        TextChatService.OnIncomingMessage = function(Message)
            return Properties
        end
    else
        local Properties = Instance.new("TextChatMessageProperties")
        Properties.Text = senderName .. ": " .. message
        TextChatService.OnIncomingMessage = function(Message)
            return Properties
        end
    end
end)

This will edit the message to either make it blank (invisible, I’m pretty sure) or there.

Please tell me if there are any errors. :slight_smile:

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