Global chat not working

I made a global chat and it does not work, so can we encode a table?

local messagingservice = game:GetService("MessagingService")
local httpsservice = game:GetService("HttpService")

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        local data = {plr.Name,tostring(msg)}
        local data2 = httpsservice:JSONEncode(data)
        messagingservice:PublishAsync("PlayerMessage",data)
    end)
end)

messagingservice:SubscribeAsync("PlayerMessage",function(message)
    local playerwhosent = message[1]
    local messageplayersent = message[2]
    if not game.Players:FindFirstChild(playerwhosent) then
        game.ReplicatedStorage.ChatMessageEvent:FireAllClients(playerwhosent,messageplayersent)
    end
end)

So it does not print any error in line 6 which is the encoding of a table, I saw some posts and heard you cannot decode a table, you can decode a string, so its possible to encode a table but not decode it? tell me how to fix this

1 Like

Why are you encoding it in the first place?

1 Like

idk can we send tables in messaging service?

Yes, I believe so, as long as you keep the message at the maximum size of 1 kB. The 2nd parameter for PublishAsync is a variant.

what is a variant? it means table,int value,string all values?

In a way yes, in this case being a table that you are trying to publish, I am confident that this is just a straight yes.

You don’t need to encode the table. You could do something along the lines of:

local message_temp = {
    ["Player"] = player.Name,
    ["Message"] = msg
}

messagingservice:PublishAsync("PlayerMessage", message_temp)

Receiving it would look like:

local subscribeSuccess, subscribeConnection = pcall(function()
    return messagingservice:SubscribeAsync("PlayerMessage", function(msg)
        local from = msg.Data["Player"]
        local message = msg.Data["Message"]
    end)
end)

can this script work to, anyway thank you for the script:

local messagingservice = game:GetService("MessagingService")
local httpsservice = game:GetService("HttpService")

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        local data = {plr.Name,tostring(msg)}
        local data2 = httpsservice:JSONEncode(data)
        messagingservice:PublishAsync("PlayerMessage",data)
    end)
end)

messagingservice:SubscribeAsync("PlayerMessage",function(message)
    local playerwhosent = message[1]
    local messageplayersent = message[2]
    if not game.Players:FindFirstChild(playerwhosent) then
        game.ReplicatedStorage.ChatMessageEvent:FireAllClients(playerwhosent,messageplayersent)
    end
end)

Are there any errors in the output? You could try wrapping the MessagingService functions within a pcall:

 plr.Chatted:Connect(function(msg)
        local data = {plr.Name,tostring(msg)}
        local publishsuccess, publishresult = pacll(function()
            messagingservice:PublishAsync("PlayerMessage",data)
        end)
        if not publishsuccess then
            print(publishresult)
        end
    end)

local subscribesuccess, subscribeconnection = pcall(function()
    return messagingservice:SubscribeAsync("PlayerMessage",function(message)
        local playerwhosent = message[1]
        local messageplayersent = message[2]
        if not game.Players:FindFirstChild(playerwhosent) then
            game.ReplicatedStorage.ChatMessageEvent:FireAllClients(playerwhosent,messageplayersent)
        end
    end)
end

It says argument missing or nil for the script i sent

Which part of the script did the error occur? Which argument is missing or nil?

it says line 15 argument missing or nil,

my game somehow how alot of errors of kill bricks so its hard to take an image of the output

i changed the script a bit,

local messagingservice = game:GetService("MessagingService")
local httpsservice = game:GetService("HttpService")

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        local success,errormessage = pcall(function()
            local data = {
                ["PlayerName"] = plr.Name;
                ["PlayerMessage"] = msg
            }
            messagingservice:PublishAsync("PlayerMessage",data)
        end)
    end)
end)

messagingservice:SubscribeAsync("PlayerMessage",function(message)
    local success,errormessage = pcall(function()
        local playerwhosent = message["PlayerName"]
        local messageplayersent = message["PlayerMessage"]
        if not game.Players:FindFirstChild(playerwhosent) then
            game.ReplicatedStorage.ChatMessageEvent:FireAllClients(playerwhosent,messageplayersent)
        end
    end)
end)

does this work?

Oh, simple mistake. You forgot to add “.Data” after message. The SubscribeAsync function should read:

messagingservice:SubscribeAsync("PlayerMessage",function(message)
    local success,errormessage = pcall(function()
        local playerwhosent = message.Data["PlayerName"]
        local messageplayersent = message.Data["PlayerMessage"]
        if not game.Players:FindFirstChild(playerwhosent) then
            game.ReplicatedStorage.ChatMessageEvent:FireAllClients(playerwhosent,messageplayersent)
        end
    end)
end)

wait it don’t work, we tried in 3 servers with 3 people, none said it worked, neither they or me see the message

image

this time no error is printed, i guess and all are updated servers only

What’s the code connected to ChatMessageEvent? MessagingService is functioning correctly, so it may be on the end that’s receiving the message.

local textservice = game:GetService("TextService")
game.ReplicatedStorage.ChatMessageEvent.OnClientEvent:Connect(function(plrname,message)
    game.StarterGui:SetCore("ChatMakeSystemMessage",{
        Text = "["..plrname.."]: "..textservice:FilterStringForBroadcast(message,game.Players.LocalPlayer.UserId);
        TextSize = 18;
        Color = Color3.fromRGB(255, 255, 255)
    })
end)

this is the local script in starter gui
image

Well, you aren’t filtering the string correctly, and you’re also attempting to filter on a Local Script, which isn’t recommended:

Script:

messagingservice:SubscribeAsync("PlayerMessage",function(message)
	local success,errormessage = pcall(function()
		local playerwhosent = message.Data["PlayerName"]
		local messageplayersent = message.Data["PlayerMessage"]
		local success, err = pcall(function()
			filtered = textservice:FilterStringAsync(messageplayersent, game.Players:GetUserIdFromNameAsync(playerwhosent))
			filteredc = filtered:GetNonChatStringForBroadcastAsync()
		end)
		if success then
			if not game.Players:FindFirstChild(playerwhosent) then
				game.ReplicatedStorage.ChatMessageEvent:FireAllClients(playerwhosent,filteredc)
			end
		else
			print(err)
		end
	end)
end)

Local Script:

local textservice = game:GetService("TextService")
game.ReplicatedStorage.ChatMessageEvent.OnClientEvent:Connect(function(plrname,message)
    game.StarterGui:SetCore("ChatMakeSystemMessage",{
        Text = "["..plrname.."]: "..message;
        TextSize = 18;
        Color = Color3.fromRGB(255, 255, 255)
	})
	print("Called")
end)
1 Like

hello sir, this dosen’t work i just tested it no error is printed from client-server side
everyone spammer but no one got any message
image

send both scripts… I tested it myself and it worked fine.