Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can messaging service be used to transfer data server to server?

People have told me that the best way to transfer/send data from one server to another is to use messaging service but I don't quite understand it. Publishing and Subscribing I don't see how that can send data at all. If someone could explain messaging service as well as how to use it to transfer data that would be appreciated.

1 answer

Here's an example, where one server is trying to find a server that needs more players This script is looking for a server

local placeId = --PlaceId
local MS = game:GetService('MessagingService')
local topic = 'FindAvailableServer'

--Some other code here

local function Start()
    debounce = true

    script.Parent.StartLabel.Text = 'Searching for Server...'

    MS:PublishAsync(topic, 'Player-'..game.Players.LocalPlayer.UserId) --Send out async with a channel to send it back
	
    local past = time()
    local GameFound = nil
    local Connection = MS:SubscribeAsync('Player-'..game.Players.LocalPlayer.UserId, function(message) --Listen in that channel
        GameFound = message
    end)
	repeat wait() until GameFound ~= nil or time() - past > 10 --Wait till 10 seconds have elapsed or a game is found
    Connection:Disconnect()
    if not GameFound then
        script.Parent.StartLabel.Text = 'Unable to locate a server. Creating one...'
        --Create a new server
        script.Parent.StartLabel.Text = 'Server Created! Teleporting...'
	--Teleport player to the server
    else
        script.Parent.StartLabel.Text = 'Server Located! Teleporting...'
        tps:TeleportToPlaceInstance(placeId, GameFound, game.Players.LocalPlayer) --Teleport player to that server
    end

    debounce = false
end

--Some more code here

This is the server checking for any requests

local MS = game:GetService('MessagingService')
local topic = 'FindAvailableServer'

while true do
	wait()
	local connected = false
	local Connection = MS:SubscribeAsync(topic, function(message) --Listen
		connected = true
		if workspace.GameInProgress.Value then
			MS:PublishAsync(message, nil) --Send back on the channel nil (Not available)
		else
			MS:PublishAsync(message, game.JobId)--Send back on the channel nil (available)
		end 
	end)
	repeat wait() until connected --Wait till connected to disconnect 
	Connection:Disconnect()
end

I'm pretty sure this is how the messaging service works. I hope this helps!

1 comment

0

Thanks but I'm still a little bit confused. I was hoping on a more in depth explanation rather than just examples.

This discussion is preserved as it was. New answers, comments, and votes are closed.