I am making something that requires cross server communication, and I found messaging service to be what I was looking for. So I tried it on my phone and on my computer, but it only worked on the device that I pressed the button on. Anybody know why?
remote.OnServerEvent:Connect(function(player, Testmessage)
local success, errormes = pcall(function()
return messagingService:SubscribeAsync("TestMessage", function(message)
print(message.Data)
end)
end)
if success then
errormes:Disconnect()
end
local Publishsuccess, Publisherrormes = pcall(function()
local function message()
Testmessage.Visible = true
end
messagingService:PublishAsync("TestMessage", message())
end)
end)
You should put this outside the remote connection.
Like this
repeat
local success, errormes = pcall(function()
return messagingService:SubscribeAsync("TestMessage", function(message)
print(message.Data)
end)
end)
wait(5)
until success
remote.OnServerEvent:Connect(function(player, Testmessage)
local Publishsuccess, Publisherrormes = pcall(function()
local function message()
Testmessage.Visible = true
end
messagingService:PublishAsync("TestMessage", message())
end)
end)
.
And also, what is this?
The function or the publishAsync()?
because the function is to make the message visible, and the publishasync i got off the api
Put the SubscribeAsync
outside the OnServerEvent
Anyways, I gave you the code already.
Okay thanks I’ll try it out right now
edit: it did the same thing as before
The code I sent you is the proper way to do it. The reason it only works for the player that did the function, is because of this
Anyways, talking about MessagingService. MessagingService:PublishAsync
only takes a string as the parameter. The message()
function returns nothing, and since you called it, it returns nil. Even if you accidentally put a ()
, you can’t pass a function.
A solution is to make action-value string or just string value itself.
messagingService:PublishAsync("TestMessage", message()) -- You are calling the function, which returns nothing
messagingService:PublishAsync("TestMessage", "Message blah #123") -- You should pass a string, or call a function that returns a string.
Then in the SubscribeAsync
, you recieve that passed data and get it using message.Data
.
local success, errormes = pcall(function()
return messagingService:SubscribeAsync("TestMessage", function(message)
print(message.Data)
end)
end)
Using that data you got, you can create cross-server events or actions. In your case, you want Testmessage
to be visible, then you can do this
local success, errormes = pcall(function()
return messagingService:SubscribeAsync("TestMessage", function(message)
print(message.Data)
if message.Data == "Message blah #123" then
Testmessage.Visible = true
end
end)
end)
Sorry for the late response, but that does make a lot more sense. thanks