#MemoryStoreService

1 messages · Page 1 of 1 (latest)

silent herald
#

Im trying to make a matchmaking system where if you click a button it puts you in a global matchmaking queue and after 4 random players join they get teleported to another place where the game starts. Ill show the script:

#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local MemoryStoreService = game:GetService("MemoryStoreService")
local TeleportService = game:GetService("TeleportService")

local joinQueueEvent = ReplicatedStorage:WaitForChild("GLSolo")
local updateQueueLabelEvent = ReplicatedStorage:WaitForChild("UpdateQueueLabelSolo")

local queue = MemoryStoreService:GetQueue("GlobalMatchQueue")
local MAX_PLAYERS = 4
local MATCH_PLACE_ID = 132817753335599

-- When a player clicks to join the queue
joinQueueEvent.OnServerEvent:Connect(function(player)
    print(player.Name .. " is joining the global queue...")

    local success, err = pcall(function()
        queue:AddAsync(player.UserId, 60) -- player stays in queue for 60 seconds
    end)

    if not success then
        warn("Queue add failed:", err)
    end
end)

Server script part 1.

#
-- Matchmaker loop
task.spawn(function()
    while true do
        task.wait(5)

        print("Matchmaker checking queue...")
        local items = {}
        local peekSuccess, peekErr = pcall(function()
            items = queue:ReadAsync(MAX_PLAYERS, false) or {} -- Peek without removing
        end)

        if peekSuccess then
            local currentCount = #items
            print("Queue has", currentCount, "players waiting.")
            
            for _, plr in ipairs(Players:GetPlayers()) do
                print("Firing")
                updateQueueLabelEvent:FireClient(plr, currentCount, MAX_PLAYERS)
            end
            -- Start match if full
            if currentCount == MAX_PLAYERS then
                local userIds = {}
                local readSuccess, readErr = pcall(function()
                    userIds = queue:ReadAsync(MAX_PLAYERS, false)
                end)

                if readSuccess then
                    print("Match ready with:", table.concat(userIds, ", "))

                    local code
                    local reserveSuccess, reserveErr = pcall(function()
                        code = TeleportService:ReserveServer(MATCH_PLACE_ID)
                    end)

                    if reserveSuccess then
                        for _, userId in ipairs(userIds) do
                            local plr = Players:GetPlayerByUserId(userId)
                            if plr then
                                TeleportService:TeleportToPrivateServer(MATCH_PLACE_ID, code, {plr})
                            end
                        end
                    else
                        warn("Server reserve failed:", reserveErr)
                    end
                else
                    warn("Queue read failed:", readErr)
                end
            end
        else
            warn("Queue peek failed:", peekErr)
        end
    end
end)

Server script part 2

#
local plr = game.Players.LocalPlayer
local gui = plr:WaitForChild("PlayerGui"):WaitForChild("MatchakingGUI")
local button = gui:WaitForChild("TextButton")
local label = gui:WaitForChild("PlayerText")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local joinQueueEvent = ReplicatedStorage:WaitForChild("GLSolo")
local updateQueueLabelEvent = ReplicatedStorage:WaitForChild("UpdateQueueLabelSolo")

button.MouseButton1Click:Connect(function()
    print("Button clicked, requesting to join queue")
    joinQueueEvent:FireServer()
end)

updateQueueLabelEvent.OnClientEvent:Connect(function(current, max)
    print("Changing Label")
    label.Text = current .. "/" .. max .. " Players"
end)

Local script

#

What happens:

  I join with 2 players - me and my alt.

on Both accounts I click the button Join queue. That works correctly as I join a queue. BUT, both accounts are not in the same queue because it shows 1/4 players on each both accounts

#

So if I had 4 accounts, each account would be on their seperate little room showing: 1/4 players.