Hi guys. I have done matchmaking system, which utilizes TeleportService:ReserveServer()
The problem I faced though is that I need along with that add some starting data for server. I have decided to do that with the use of MemoryStoreService
. That’s what I have gotten:
Lobby script
local Success, Data, SortKey = pcall(function()
local Data, SortKey = MemoryStore:UpdateAsync(QueueKey, function(Data, SortKey)
if (#Data.Players >= ServerSizePlayerAmount[ServerSize] or Data.Timeout - DateTime.now().UnixTimestamp <= 0) and not Data.Server then
local ServerCode, ServerId = TeleportService:ReserveServer(PlaceId)
Data.Server = ServerCode
SortKey = -1
local Repeats = 10
repeat
local Success, Error = pcall(function()
print("Server id: " .. tostring(ServerId))
return ReservedServersMS:UpdateAsync(tostring(ServerId), function(ServerData)
if not ServerData then
ServerData = {}
ServerData.Gamemode = Gamemode
ServerData.Players = #Data.Players
ServerData.QueueId = QueueKey
ServerData.ServerSize = ServerSize
return ServerData
end
end, 300)
end)
Repeats -= 1
until Success or Repeats <= 0
return Data, SortKey
end
end, 3600)
return Data, SortKey
end)
if Success then
for i = 1, #QueueData.Players, 1 do
PlayerQueueKeys[QueueData.Players[i]].LeaveReason = "Teleport"
print("Teleporting player: " .. QueueData.Players[i].Name)
end
if Data.Server then
local TeleportOptions = Instance.new("TeleportOptions")
TeleportOptions.ReservedServerAccessCode = Data.Server
TeleportOptions:SetTeleportData({
Parties = QueueData.Parties
})
TeleportService:TeleportAsync(PlaceId, QueueData.Players, TeleportOptions)
end
end
Round script
local PlayersAmount, Gamemode, QueueKey, PlayerServer
if RunService:IsStudio() then
PlayersAmount = 1
else
local Success, Data
local Repeats = 30
repeat
task.wait(1)
Success, Data = pcall(function()
return ReservedServersMS:GetAsync(tostring(game.PrivateServerId))
end)
print(Success, Data)
Repeats -= 1
until Success and Data or Repeats <= 0
warn(Success, Data)
if Success and Data then
print(Success, Data)
PlayersAmount = Data.Players
Gamemode = Data.Gamemode
QueueKey = Data.QueueId
PlayerServer = Data.ServerSize
else
warn("No queue found") --ALWAYS goes this way, due to timeout of repeats.
PlayersAmount = 49
end
end
Why server fails to find correct initialization data? What I’m doing wrong here?