Sends a message whenever a player joins and the other script subscribes to the topic whenever a player gets added and then unsubscribes if the ancestry changes.
I am not sure though if that script in the guide is duplicating the amount of subscription to the “FriendServerEvent”
but… eitherways
Now, since there’s a script that publishes the message and another script that subscribes to the message. How is it ensured that it works?
Because, what happens if it first sends the message with “PublishAsync” before the other script even called “SubscribeAsync”?
What I tested, is that if you first send the PublishAsync, without being subscribed to the topic sending to, it won’t receive it, obviously. So I am not quite sure about that tutorial in that wiki.
Use a GlobalDataStore and have each server check the message from time to time. I did something like this with GetKeyValue() in Code.org multiple times.
It very much is not the best solution because it requires constant grabbing and delays the script, so I suggest using it as a separate script:
Script in any place possible:
--setting it on one script
local datastore = game:GetService("DataStoreService"):GetDataStore("Message")
datastore:SetAsync("SentMessage", "Hello World!")
Script in ServerScriptStorage:
--getting it on another
local senddata = game:GetService("ReplicatedStorage").RemoteEvent
local datastore = game:GetService("DataStoreService"):GetDataStore("Message")
while wait() do
local success, data = pcall(function()
return datastore:GetAsync("SentMessage")
end)
if not success then
repeat
local success, data = pcall(function()
return datastore:GetAsync("SentMessage")
end)
until success
end
if data then
--if data found, then send it and reset the message
senddata:FireAllClients(data)
--should fire on all clients "Hello World!" based on the setting from top script
datastore:SetAsync("SentMessage", nil)
end
end
But one thing is for sure, it’s a lot more complex than on Code.org’s Game Lab.