How to find the oldest server in your game?

Hello!
I am using MessagingService and I want one server to send a message to all the other servers. I want that server to be the oldest server to send the message. How would I do that?

Thanks!

4 Likes

You could hold a variable capturing os.time() when the server starts and reference that for how many seconds it’s been alive. Finding the oldest might entail another MS topic where each broadcasts it’s id and time alive, a table is formed and the id associated to the oldest one alive is told to do the work.

3 Likes

@Dev_Skylab
You can actually use this

workspace.DistributedGameTime

Wiki: Workspace.DistributedGameTime

1 Like

Oh wow, I will definitely use this but how would i loop through all the servers to look for the highest value?

Probably use memory store service. This can help you
https://developer.roblox.com/en-us/articles/memory-store

1 Like
local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("SortedMap1")
 
local messageRate = 5 -- message every 5 seconds

local function Loop()
	task.delay(messageRate, Loop)
	local currentTime = workspace.DistributedGameTime
	local success, value = pcall(sortedMap.UpdateAsync, sortedMap, "Key", function(value)
		if value == nil then return currentTime end
		if value < currentTime then return currentTime end
		return nil
	end, messageRate + 1)
	if success == false then return end
	if value == nil then return end

	-- send a message to all other servers here

end
Loop()

or another method

local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("SortedMap1")
 
local messageRate = 5 -- message every 5 seconds

local function Loop()
	task.delay(messageRate, Loop)

	-- 128 might be overkill but that's the max amount of characters your allowed for a key
	local key = string.format("%0128d", workspace.DistributedGameTime)

	-- save the servers jobid with the time as a key
	local success, value = pcall(sortedMap.SetAsync, sortedMap, key, game.JobId, messageRate + 1)

	-- get the first server from the sorted map
	local success, value = pcall(sortedMap.GetRangeAsync, sortedMap, Enum.SortDirection.Descending, 1)

	-- if we are not the oldest server exit
	if value[1] ~= game.JobId then return end

	-- send a message to all other servers here, we are the oldest

end
Loop()
``
4 Likes

Here is also code to format the server time to HMS

loc curr = 5
("%02i:%02i:%02i"):format(curr/3600%3600,curr/60%60,curr%60)

Once the server has been running for 99 hours then it will not sort correctly if you format the key like that

I appreciate the time that u put into this. I will try to modify it to my own liking!

i was talking about formatting it AFTER you retrieve the time

Master Server // Cross Server MSS - Resources / Community Resources - DevForum | Roblox

Master server will be oldest,

if IS_MASTER then
    for _, Server in pairs(Servers) do
        if Server.IsLocal then continue end
        Server.Send({["Name"]="Print",["Data"]="Message"})
    end
end

Hey, I ran into an error because I am not sure what “currentTime” means in the first script.

Oops I fixed it if you go back up I edited the code