if point.difficulty == "Simple" then
local MessagingService = game:GetService("MessagingService")
local AnnouncementTopic = "GlobalAnnouncement"
MessagingService:SubscribeAsync(AnnouncementTopic, function(message)
local data = message.Data
if data.Message then
ReplicatedStorage.Message:FireAllClients(data.Message, data.Color)
end
end)
MessagingService:PublishAsync(AnnouncementTopic, {
Message = "[GLOBAL] " .. player.Name .. " has passed the difficulty " .. point.difficulty .. " and therefore beaten the game.. for now.",
Color = point.color
})
else
ReplicatedStorage.Message:FireAllClients(
player.Name .. " has passed the difficulty " .. point.difficulty .. "!",
point.color
)
end
This is not working. My goal is to get the RemoteEvent to fire in every server but it won’t work.
if point.difficulty == "Simple" then
local MessagingService = game:GetService("MessagingService")
local AnnouncementTopic = "GlobalAnnouncement"
MessagingService:SubscribeAsync(AnnouncementTopic, function(message)
local data = game:GetService("HTTPSerivce"):JSONDecode(message)
if data.Message then
ReplicatedStorage.Message:FireAllClients(data.Message, data.Color)
end
end)
MessagingService:PublishAsync(AnnouncementTopic, game:GetService("HTTPService"):JSONEncode({Message = "[GLOBAL] " .. player.Name .. " has passed the difficulty " .. point.difficulty .. " and therefore beaten the game.. for now.", Color = point.color)})
else
ReplicatedStorage.Message:FireAllClients(
player.Name .. " has passed the difficulty " .. point.difficulty .. "!",
point.color
)
end
Something like this. I am not too much of a pro at httpservice but this should work?
It should be a JSON enconded dictionary containing data you need to use for the global announcement, like the player who made it, and the text. You can decode it in SubscribeAsync.
Yeah, but I am confused. What should PublishAsync() contain then?
This is how it currently looks:
MessagingService:PublishAsync(
AnnouncementTopic,
HttpService:JSONEncode({
Message = "[GLOBAL] " ..
player.Name ..
" has passed the difficulty " ..
point.difficulty ..
" and therefore beaten the game.. for now.",
Color = point.color,
})
)
And this is how SubscribeAsync() looks:
MessagingService:SubscribeAsync(AnnouncementTopic, function(message)
local data = HttpService:JSONDecode(message)
local announcement = HttpService:JSONEncode({
Message = "[GLOBAL] " ..
player.Name ..
" has passed the difficulty " ..
point.difficulty ..
" and therefore beaten the game.. for now.",
Color = point.color,
})
if data.Message then
ReplicatedStorage.Message:FireAllClients(data.Message, data.Color)
end
end)
local DefaultMessages = {
BeatGame = "{player} has passed the difficulty %s and beat the game... for now.",
Default = "{player}: %s",
}
local data = {
Name = player.Name,
UserId = player.UserId
Type = "BeatGame",
Args = {point.difficulty},
}
MessagingService:PublishAsync(AnnouncementTopic, HttpService:JSONEncode(data))
MessagingService:SubscribeAsync(AnnouncementTopic, function(m)
local data = HttpService:JSONDecode(m.Message)
local f = DefaultMessages[data.Type] or DefaultMessages.Default
f = f:gsub("{player}", "@" .. data.Name)
for i, v in pairs(data.Args) do
data.Args[i] = tostring(v)
end
f = f:format(unpack(data.Args))
ReplicatedStorage.Message:FireAllClients(f, Color3.new(1,1,0))
end)
if point.difficulty == "Simple" then
local DefaultMessages = {
BeatGame = "{player} has passed the difficulty %s and beat the game... for now.",
Default = "{player}: %s",
}
local MessagingService = game:GetService("MessagingService")
local AnnouncementTopic = "GlobalAnnouncement"
local data = {
Name = player.Name,
UserId = player.UserId,
Type = "BeatGame",
Args = { point.difficulty },
}
MessagingService:PublishAsync(AnnouncementTopic, HttpService:JSONEncode(data))
MessagingService:SubscribeAsync(AnnouncementTopic, function(m)
local data = HttpService:JSONDecode(m.Message)
local f = DefaultMessages[data.Type] or DefaultMessages.Default
f = f:gsub("{player}", "@" .. data.Name)
for i, v in pairs(data.Args) do
data.Args[i] = tostring(v)
end
f = f:format(unpack(data.Args))
ReplicatedStorage.Message:FireAllClients(f, Color3.new(1, 1, 0))
end)
else
ReplicatedStorage.Message:FireAllClients(
player.Name .. " has passed the difficulty " .. point.difficulty .. "!",
point.color
)
end
For some of the respondents above, please stop suggesting that dictionaries can’t be passed to PublishAsync: read the docs and the error message. The error keyphrase is can only accept valid UTF-8 characters. OP, with your original code, what is the type of color? You can add it just before publishing your message: print(typeof(point.color)).
PublishAsync can only accept valid UTF-8 characters and serialisable datatypes, so I have a strong feeling that you’re sending a Color3 through PublishAsync and it can’t serialise the colour.
Sorry for wrong response, i think what he said is true. You should be able to send dictionaries but turn your color3 into a hex value, so it fits UTF-8.