Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
4 years ago
Edited 4 years ago

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!

0
Thanks but I'm still a little bit confused. I was hoping on a more in depth explanation rather than just examples. Bl_ueHistory 94 — 4y
Ad

Answer this question