Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How should I do a matchmaking system where you enter your friends room code to play with them?

Asked by 3 years ago

I have been trying to do a matchmaking system where you enter your friends room code to play with them.

Here's my broken code:

local replicatedStorage = game:GetService("ReplicatedStorage")
local teleportService = game:GetService("TeleportService")
local dataStoreService = game:GetService("DataStoreService")

local joinRoom = replicatedStorage:WaitForChild("JoinRoom")
local createRoom = replicatedStorage:WaitForChild("CreateRoom")
local play = replicatedStorage:WaitForChild("Play")

local roomCodes = dataStoreService:GetOrderedDataStore("RoomCodes")

function loadCodes()
    local roomCodesTable

    local s = pcall(function()
        roomCodesTable = roomCodes:GetSortedAsync(false, math.huge)
    end)

    if not s then
        wait(1)
        loadCodes()
    end

    return roomCodesTable
end

function saveCode(code)
    local s = pcall(function()
        roomCodes:SetAsync(code)
    end)

    if not s then
        wait(1)
        saveCode(code)
    end
end

play.OnServerEvent:Connect(function(player)
    player.PlayerGui.TeleportGui.RoomCode:Destroy()
    player.PlayerGui.TeleportGui.Enabled = true

    teleportService:Teleport(6978966233, player)
end)

joinRoom.OnServerEvent:Connect(function(player, code)
    local codes = loadCodes()
    local codeInfo = table.find(codes, code)

    print(codeInfo[1], codeInfo[2])
    if codeInfo then
        teleportService:TeleportToPrivateServer(6978966233, codeInfo[2], {player})
    end
end)

createRoom.OnServerEvent:Connect(function(player)
    local roomTeleportCode = teleportService:ReserveServer(6978966233)
    local roomCode = string.sub(roomTeleportCode, 1, 6)

    print(roomCode, roomTeleportCode)
    player.PlayerGui.TeleportGui.RoomCode.Text = "Your room code is "..roomCode
    player.PlayerGui.TeleportGui.Enabled = true

    saveCode({roomCode, roomTeleportCode})
    teleportService:TeleportToPrivateServer(6978966233, roomTeleportCode, {player})
end)

This is a server script placed in serverscriptservice

Answer this question