Checking for values from different servers

I’ve been wanting to make a weather system, and I plan to use MessagingService. I’m just stumped on how I would check all servers for a value, if all return false, then the server who checked for it (they all will check) will choose a random server to get priority, how would I do this?

1 Like

I really like this question!

So if you are deadset on just using Messaging Service then my idea was that every 5 mins, all of the servers would clear their current leader, then, the first server to ping everyone in the next 10-30sec gets leadership.
This is what that would look like:

local MessagingService = game:GetService("MessagingService")

module.CurrentLeader = ""

MessagingService:SubscribeAsync("newLeader",function(leader)
	module.CurrentLeader = leader
end)

local newLeaderEveryXsec = 60 * 5
local timeToWaitToDeclare = {min = 10, max = 30} -- secs
spawn(function()
	while true do
		local secToWait = newLeaderEveryXsec - (os.time()%newLeaderEveryXsec)
		print(secToWait, newLeaderEveryXsec - secToWait)
		if newLeaderEveryXsec - secToWait > timeToWaitToDeclare.min/2 then task.wait(secToWait) continue end

		module.CurrentLeader = nil
		task.delay(math.random(timeToWaitToDeclare.min, timeToWaitToDeclare.max), pcall, function()
			if not module.CurrentLeader then
				module.CurrentLeader = ""
				MessagingService:PublishAsync("newLeader", game.JobId)
			end
		end)
		wait(timeToWaitToDeclare.min)
	end
end)


print("loaded leader thingu")
return module
2 Likes

That would be good. I’ll see how it would fit. I probably will make the leader switch way longer. Thanks for your help tho!

Sorry for the dumb question but how would I also make it send over the concurrent value on the weather?
so basically I want the value from workspace, workspace.Weather.Value to be sent over to other servers so they can sync up.

good question, I would setup another spawn loop with a pcall, that checks if we are the leader, then send off the data:

MessagingService:SubscribeAsync("weatherUpdate",function(message :message)
    local weather = message.Data
    workspace.Weather.Value = weather
end)

local sendDataEveryXsec = 30
spawn(function()
    while true do wait(sendDataEveryXsec)
        if module.CurrentLeader ~= game.JobId then continue end
        local good, result = pcall(function() --put code that processes weather here so errs are caught
            MessagingService:PublishAsync("weatherUpdate", workspace.Weather.Value)
        end)
        if not good then warn("weatherUpdate Fail:", result) end
    end
end
Full Script
local module={}
local MessagingService = game:GetService("MessagingService")
export type message = {Data :any; Sent :typeof(os.time());}

module.CurrentLeader = ""

MessagingService:SubscribeAsync("newLeader",function(leader :message)
    module.CurrentLeader = leader.Data
end)

local newLeaderEveryXsec = 60 * 1
local timeToWaitToDeclare = {min = 10, max = 20} -- secs


spawn(function()
    while true do
        local secToWait = newLeaderEveryXsec - (os.time()%newLeaderEveryXsec)
        if secToWait > timeToWaitToDeclare.min/2 then task.wait(secToWait) continue end
        module.CurrentLeader = nil
        task.delay(math.random(timeToWaitToDeclare.min, timeToWaitToDeclare.max), pcall, function()
            if not module.CurrentLeader then
                MessagingService:PublishAsync("newLeader", game.JobId)
            end
        end)
    end
end)


MessagingService:SubscribeAsync("weatherUpdate",function(message :message)
    local weather = message.Data
    workspace.Weather.Value = weather
end)

local sendDataEveryXsec = 30
spawn(function()
    while true do wait(sendDataEveryXsec)
        if module.CurrentLeader ~= game.JobId then continue end
        local good, result = pcall(function() --put code that processes weather here so errs are caught
            MessagingService:PublishAsync("weatherUpdate", workspace.Weather.Value)
        end)
        if not good then warn("weatherUpdate Fail:", result) end
    end
end)

return module
1 Like

Thank you!!! Exactly what I wanted, thanks for your help!

It seems to look functional and I hope it is, but how would I use it? It only lets me use CurrentLeader which always returns nothing. I tried to require with it, but nothing much changed