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

Matchmaking system making unknown errors?

Asked by 1 year ago

Making a matchmaking service that works in the playergui. However it returns to me 2 errors. I cant seem to figure out what the errors are... 1st error: Unable to cast value to Object - Server - MatchMakingHandler:20 2nd error: attempt to index number with 'Name' - Server - MatchMakingHandler:74

the script is located inside a button in the startergui. the updatestatus text is located inside the exact same gui, its also a localscript while the main script is a server script. Is there an issue with using normal scripts inside the startergui?

local GameId = 11470859073 --Place Id

local back = script.Parent.Parent.Parent.BackButton

local TPS = game:GetService("TeleportService")

local PlayersInQueue = {}

local MinPlayers = 1
local MaxPlayers = 10

local isMatchmaking = false

function Matchmake()
    if isMatchmaking == false then
        isMatchmaking = true

        repeat wait()
            for i,v in pairs(PlayersInQueue) do
                game.ReplicatedStorage.UpdateStatus:FireClient(v,"Waiting for at least "..tostring(MinPlayers).." players to join the Queue.")
            end
        until #PlayersInQueue == MinPlayers

        --enough players to start

        for i=0,120,1 do
            if #PlayersInQueue == 0 then
                isMatchmaking = false
                --players have left the game
                return
            end
            if #PlayersInQueue == MaxPlayers then
                break
            end

            for _,value in pairs(PlayersInQueue) do
                game.ReplicatedStorage.UpdateStatus:FireClient(value,"Teleporting in: ("..tostring(120-i)..") seconds. Players in queue: ("..tostring(#PlayersInQueue)..")")
            end

            wait(1)
        end

        --finished waiting, teleporting players now

        local Server = TPS:ReserveServer(GameId)
        TPS:TeleportToPrivateServer(GameId,Server,PlayersInQueue)


        wait(3)
        PlayersInQueue = {}
        isMatchmaking = false
    end
end

script.Parent.MouseButton1Down:Connect(function(player)
    if #PlayersInQueue == MaxPlayers then return end
    if not table.find(PlayersInQueue,player) then
        table.insert(PlayersInQueue,player)
    end
    Matchmake()
end)

game.Players.PlayerRemoving:Connect(function(player)
    for i,v in pairs(PlayersInQueue) do
        if v.Name == player.Name then
            table.remove(PlayersInQueue,i)
        end
    end
end)


back.MouseButton1Down:Connect(function(player)
    for i,v in pairs(PlayersInQueue) do
        if v.Name == player.Name then
            table.remove(PlayersInQueue,i)
        end
    end
end)

Any help is appreciated!

Answer this question