Need help with matchmaking table math

hi im ctg, i need help

for i, v in pairs(GlobalMatchmaking) do
			if typeof(v) == "table" then
				table.foreach(v, print) 
				
				if #GlobalMatchmaking[i] + #GlobalMatchmaking[i] == maxPlayer then	
					table.remove(GlobalMatchmaking, table.find(GlobalMatchmaking, i))
					local plr = Players:FindFirstChild(v)
					if plr then
						replicateStorage.MatchmakingClient:FireClient(plr, "Matched", convertToHMS(times))	
					end

					timer.Text = "Matched"
					partyId.Value = ""

					Party.Teleport(plr, placeId.Value)
					Party.Matched(matchMakingPlace)
					break
				end
			end	
		end

the script will loop all table inside matchmaking table then find any random party
example: party 1 have 3 players, party 2 have 1 player and party 3 have 1 player

i want it random any party to it = 4

I’m not sure what you want it to do. Do you want it to combine parties into groups of exactly 4?

yeah combine 2 party become 4, also find all party then random every party to it become 4

Sure, here’s an example of how you can modify your existing code to randomly select a party with a total of 4 players:

math.randomseed(os.time())

function table.shuffle(t)
    local n = #t
    while n > 1 do
        local k = math.random(n)
        t[n], t[k] = t[k], t[n]
        n = n - 1
    end
    return t
end

-- Shuffle the GlobalMatchmaking table
table.shuffle(GlobalMatchmaking)

-- Loop through each party in GlobalMatchmaking
for i, party in pairs(GlobalMatchmaking) do
    if type(party) == "table" then
        -- Calculate the total number of players in this party and the next party
        local nextPartyIndex = i + 1
        local totalPlayers = #party
        while nextPartyIndex <= #GlobalMatchmaking and type(GlobalMatchmaking[nextPartyIndex]) == "table" do
            totalPlayers = totalPlayers + #GlobalMatchmaking[nextPartyIndex]
            nextPartyIndex = nextPartyIndex + 1
        end
        
        -- Check if the total number of players is 4
        if totalPlayers == 4 then
            -- Select this party and remove it from GlobalMatchmaking
            table.remove(GlobalMatchmaking, i)
            
            -- Randomly select a player from the party to teleport
            local plr = game.Players:GetPlayerFromCharacter(party[math.random(#party)])
            if plr then
                -- Fire the "Matched" event on the client and teleport the player
                replicateStorage.MatchmakingClient:FireClient(plr, "Matched", convertToHMS(times))
                timer.Text = "Matched"
                partyId.Value = ""
                Party.Teleport(plr, placeId.Value)
                Party.Matched(matchMakingPlace)
            end
            
            -- Exit the loop
            break
        end
    end
end

Note that this code shuffles the GlobalMatchmaking table using the table.shuffle function, which I defined at the beginning of the script. This ensures that the order of the parties is randomized before the loop starts.

The modified loop calculates the total number of players in the current party and any subsequent parties until the total number of players is equal to 4. If a party with 4 players is found, the script selects a random player from the party, teleports them to the desired place, and exits the loop. If no party with 4 players is found, the loop completes without doing anything.

This might help you a lil

local plr = game.Players:GetPlayerFromCharacter(party[math.random(#party)])

are u understand wat im say?

Doing this doesnt actually make a new function within the table library, in fact It wont even do it at all because you are attempting to Modify a read only table which will result in an error.