Global Chat: "The sender must be connected to the same server"

I’m trying to make a global chat but it errors on objectSuccess pcall and I don’t know how to fix it.

My script fails on the objectSuccess pcall() and results say “The sender must be connected to the same server”, my game is a singleplayer.

-- VARIABLES --
local TextChatService = game:GetService("TextChatService")
local MessagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local Players = game:GetService("Players")

local chattedEvent = ReplicatedStorage.RemoteEvents:WaitForChild("OtherEvents").ChattedEvent
local topic = "GlobalChat"


-- SCRIPTS --
chattedEvent.OnServerEvent:Connect(function(player, message)
    local dataToBeSent = player.Name..'\\'..message
    MessagingService:PublishAsync(topic, dataToBeSent)
    print("Published!!")
end)

MessagingService:SubscribeAsync(topic, function(message)

    -- Variable
    local fromPlayer, playerMessage = string.match(message.Data, "(.-)\\(.*)")

    -- Script

    if not table.find(Players:GetPlayers(), Players:GetPlayerByUserId(Players:GetUserIdFromNameAsync(fromPlayer))) then
        for _, player in Players:GetPlayers() do
            local objectSuccess, object = pcall(function()
                return TextService:FilterStringAsync(
                    playerMessage, 
                    Players:GetUserIdFromNameAsync(fromPlayer), 
                    Enum.TextFilterContext.PublicChat
                )
            end)

            if not objectSuccess then print("not object success") return end

            local filterSuccess, result = pcall(function()
                return object:GetChatForUserAsync(player.UserId)
            end)
            if not filterSuccess then print("not filter success") return end
            chattedEvent:FireClient(player, fromPlayer, result)
        end
    end

end)

help is appreciated thanks :slight_smile:

FilterStringAsync only works when the sending player is in the same place, as is told by the error message - you can’t filter a string from a player in a different server instance. You will need to filter the string and get the broadcast string before sending it through PublishAsync.

1 Like

So what filtering method do I use for this? - I’m sorry I don’t really understand fully on what you meant on the last sentence.

Your code will remain mostly the same, just instead of calling FilterStringAsync in SubscribeAsync you would call it before PublishAsync (in the ChattedEvent). For the TextFilterResult returned by FilterStringAsync, you would use GetNonChatStringForBroadcastAsync instead of GetChatForUserAsync for two reasons: your remote has no validation (security measures) and it’s a global chat delivered to all servers regardless of chat settings with another user.

1 Like

So how would the filterSuccess pcall() get the result of the TextFilter (objectSuccess pcall) if its in the ChattedEvent?

Sorry if i ask many questions, i’m still fairly new to using pcall()