Memory Store Serverlist

Memory Store Server Index

  • Doesn’t work in studio
  • Gui incomplete

Serverscript:

local MemoryStoreService = game:GetService("MemoryStoreService")
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local RunService = game:GetService("RunService")
local ServerIndexMap = MemoryStoreService:GetSortedMap("ServerIndex")
local ServerKey = tostring(game.JobId)


local Admins = {
	[000000] = true, -- UserId
}


function GetAllServers(map)
	local ServerItems = {}
	local StartFrom = nil
	while true do
		local Items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, StartFrom)
		for _, Item in ipairs(Items) do
			table.insert(ServerItems, {Item.key, HttpService:JSONDecode(Item.value)})
		end
		if #Items < 100 then
			break
		end
		StartFrom = Items[#Items].key
		wait(3)
	end
	return ServerItems
end


function FlushServers(map)
	local StartFrom = nil
	while true do
		local Items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, StartFrom)
		for _, Item in ipairs(Items) do
			map:RemoveAsync(Item.key)
		end
		if #Items < 100 then
			break
		end
		StartFrom = Items[#Items].key
		wait(3)
	end
end


function UploadToIndex(map)
	local Data = {
		["Players"] = #game.Players:GetPlayers(),
		["Uptime"] = math.round(RunService.Stepped:Wait())
	}
	map:SetAsync(ServerKey, HttpService:JSONEncode(Data), 180)
end


game.Players.PlayerAdded:Connect(function(Player)
	UploadToIndex(ServerIndexMap)
	if Admins[Player.UserId] then
		Player.Chatted:Connect(function(msg)
			if msg:lower() == "!flush" then
				print("Flushing Server Index")
				FlushServers(ServerIndexMap)
				return
			end
			if msg:lower() == "!upload" then
				print("Uploading to Server Index")
				UploadToIndex(ServerIndexMap)
				return
			end
		end)
	end
end)
game.Players.PlayerRemoving:Connect(function()
	UploadToIndex(ServerIndexMap)
end)


game:BindToClose(function()
	ServerIndexMap:RemoveAsync(ServerKey)
end)


local Servers = {}
local GetServerIndexRemote = Instance.new("RemoteFunction")
GetServerIndexRemote.Name = "GetServerIndexRemote"
GetServerIndexRemote.OnServerInvoke = function(Player)
	return Servers
end
GetServerIndexRemote.Parent = ReplicatedStorage
local TeleportToServerRemote = Instance.new("RemoteFunction")
TeleportToServerRemote.Name = "TeleportToServerRemote"
TeleportToServerRemote.OnServerInvoke = function(Player, JobId)
	if JobId == nil then return end
	if type(JobId) ~= "string" then return end
	for Index, ServerObj in pairs(Servers) do
		if ServerObj[1] == JobId then
			TeleportService:TeleportToPlaceInstance(game.PlaceId, JobId, Player)
			break
		end
	end
end
TeleportToServerRemote.Parent = ReplicatedStorage



while true do
	UploadToIndex(ServerIndexMap)
	Servers = GetAllServers(ServerIndexMap)
	wait(10)
end

Localscript for gui:

local Template = script.Parent.ScrollingFrame.Template:Clone()
script.Parent.ScrollingFrame.Template:Destroy()

local function hms(seconds)
	return string.format("%02i:%02i:%02i", seconds/60^2, seconds/60%60, seconds%60)
end

local GetServerIndexRemote = game:GetService("ReplicatedStorage"):WaitForChild("GetServerIndexRemote")
local TeleportToServerRemote = game:GetService("ReplicatedStorage"):WaitForChild("TeleportToServerRemote")
while true do
	local Servers = GetServerIndexRemote:InvokeServer()
	if Servers == nil then continue end
	script.Parent.ScrollingFrame:ClearAllChildren()
	local Index = 0
	for _, ServerObj in pairs(Servers) do
		local GuiEntry = Template:Clone()
		GuiEntry.Visible = true
		GuiEntry.TextLabel.Text = "Players: "..ServerObj[2].Players.."  //  Uptime: "..hms(ServerObj[2].Uptime)
		GuiEntry.Position = UDim2.fromScale(0, 0.025*Index)
		if ServerObj[1] == game.JobId then
			GuiEntry.TextButton.BackgroundColor3 = Color3.fromRGB(86, 86, 86)
			GuiEntry.TextButton.Text = "Joined"
		else
			GuiEntry.TextButton.MouseButton1Click:Connect(function()
				TeleportToServerRemote:InvokeServer(ServerObj[1])
			end)
		end
		GuiEntry.Parent = script.Parent.ScrollingFrame
		Index += 1
	end
	wait(1)
end

ServerIndex.rbxl (43.6 KB)

image

15 Likes