Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my MessagingService Global Chat Messages not replicating in other servers?

Asked by 4 years ago

I made a global chat using messagingservice, i've added a filter and everything. But the problem is that my messages aren't showing in other servers, And roblox studio isnt showing a single error in the output, Here are the scripts:

ChatSystem, ServerScript

local MS = game:GetService("MessagingService")

local Topic = "GlobalChat"

game.Players.PlayerAdded:Connect(function(plr)
    -- recieve chat
    local subscribeSuccess, subscribeConnection = pcall(function()
        return MS:SubscribeAsync(Topic, function(message)
            game.ReplicatedStorage.Events.RecieveChat:Fire(message.Data)
        end)
    end)
    if subscribeSuccess then
        plr.AncestryChanged:Connect(function()
            subscribeConnection:Disconnect()
        end)
    end
    -- send chat
    game.ReplicatedStorage.Events.SendChat.Event:Connect(function(chat)
        local publishSuccess, publishResult = pcall(function()
            local message = {plr.Name,chat}
            MS:PublishAsync(Topic, message)
        end)
        if not publishSuccess then
            print(publishResult)
        end
    end)
end)

RecieveChat, ServerScript

local TS = game:GetService("TextService")
local PLRS = game:GetService("Players")
local LP = script.Parent.Parent.Parent.Parent
local MaxChats = 40
local CN = 0
local cache = {}

function GetUserIdFromUsername(name)
    if cache[name] then 
        return cache[name] 
    end
    local player = PLRS:FindFirstChild(name)
    if player then
        cache[name] = player.UserId
        return player.UserId
    end 
    local id
    pcall(function()
        id = PLRS:GetUserIdFromNameAsync(name)
    end)
    cache[name] = id
    return id
end

local function FilterText(plr,chat)
    local TextObject
    local success, errorMessage = pcall(function()
        TextObject = TS:FilterStringAsync(chat, GetUserIdFromUsername(plr))
    end)
    if success then
        return TextObject
    end
    return nil
end

local function GetFilteredMessage(textObject, UserId)
    local FilteredMessage
    local success, errorMessage = pcall(function()
        FilteredMessage = textObject:GetChatForUserAsync(UserId)
    end)
    if success then
        return FilteredMessage
    end
    return nil
end

local function ColorChat(length)
    if length <= 3 then
        return Color3.fromRGB(38, 255, 136)
    elseif length == 4 then
        return Color3.fromRGB(84, 255, 5)
    elseif length == 5 then
        return Color3.fromRGB(227, 255, 83)
    elseif length == 6 then
        return Color3.fromRGB(253, 41, 67)
    elseif length == 7 then
        return Color3.fromRGB(59, 41, 255)
    elseif length == 8 then
        return Color3.fromRGB(220, 19, 255)
    elseif length == 9 then
        return Color3.fromRGB(255, 65, 65)
    elseif length == 10 then
        return Color3.fromRGB(255, 1, 13)
    elseif length == 11 then
        return Color3.fromRGB(28, 255, 161)
    elseif length == 12 then
        return Color3.fromRGB(255, 48, 138)
    elseif length == 13 then
        return Color3.fromRGB(255, 156, 69)
    elseif length == 14 then
        return Color3.fromRGB(17, 0, 255)
    elseif length >= 15 then
        return Color3.fromRGB(0, 255, 17)
    end
    return Color3.fromRGB(255, 65, 65)
end

local function FindLastChat(frame)
    local lastchat = nil
    for i,v in pairs(frame:GetChildren()) do
        if v.Position == UDim2.new(0,0,0.975,0) then
            lastchat = v
        end
    end
    return lastchat
end

game.ReplicatedStorage.Events.RecieveChat.Event:Connect(function(data)
    local chats = script.Parent.ChatBox.Chats
    local chat = game.ReplicatedStorage.Chat:Clone()
    local plr,msg = data[1],data[2]
    if GetFilteredMessage(FilterText(plr,msg),LP.UserId) ~= nil then
        CN = CN + 1
        chat.Name = "Chat"..CN
        local message = GetFilteredMessage(FilterText(plr,msg),LP.UserId)
        if plr == "System" then
            chat.Username.Text = "{"..plr.."}:"
        else
            chat.Username.Text = "["..plr.."]:"
        end
        chat.ChatInfo.Text = message
        chat.Username.TextColor3 = ColorChat(string.len(plr))
        chat.Parent = chats
        if FindLastChat(chats) ~= nil then
            FindLastChat(chats):Destroy()
        end
        for i,v in pairs(chats:GetChildren()) do
            v.Position = UDim2.new(v.Position.X.Scale,v.Position.X.Offset,v.Position.Y.Scale+0.025,v.Position.Y.Offset)
        end
        chat.Position = UDim2.new(0,0,0,0)
    end
end)

SendChat, ServerScript

local cd = script.Parent.Cooldown
local plr = script.Parent.Parent.Parent.Parent

script.Parent.ChatText.ChatSend.MouseButton1Click:Connect(function()
    if cd.Value == 0 then
        game.ReplicatedStorage.Events.SendChat:Fire(script.Parent.GetText:InvokeClient(plr))
        cd.Value = 10
        for i = 1,10 do
            cd.Value = cd.Value - 1
            script.Parent.ChatText.ChatSend.Text = (cd.Value+1).."s"
            wait(1)
        end
        script.Parent.ChatText.ChatSend.Text = "Send"   
    end
end)

If you can see the problem then please reply.

Answer this question