Local Script Matchmaking Issue

This is a simple matchmaking local script:

local OnTeleportation = false

local function UpdateButton()
   print(OnTeleportation)
end

        PlayButton.MouseButton1Click:Connect(function()
    if not debounce then
        debounce = true

        if not OnMatchmaking then
            OnMatchmaking = true

            UpdateButton()
            task.wait(3)

            Matchmaking:FireServer(OnMatchmaking)

            repeat task.wait() until QueuePlayers == 2 or OnMatchmaking == false

            if QueuePlayers == 2 then
                                OnTeleportation = true
                UpdateButton()

                Matchmaking:FireServer(OnMatchmaking, "Teleportation")
            end
        else
            OnMatchmaking = false

            Matchmaking:FireServer(OnMatchmaking)

            MessageHandler.IssueMessage("Matchmaking canceled.", 2)

            task.spawn(function()
                for i = 3, 0, -1 do
                    PlayButton.Text = i.."s"
                    task.wait(1)
                end

                PlayButton.Text = "Play"
                debounce = false
            end)
        end
    end
end)

The following issue happens:
There is a Player1 that clicks on the button, he joins the queue first and everything goes on correctly. But if there is a Player2 and there is someone on the queue already, when the UpdateButton function is called, it will simply return that OnTeleportation is false, when it should be true.

Please help me fix this issue!

The issue you’re encountering seems to be related to the timing of when UpdateButton is called. Specifically, you set OnTeleportation to true, but the UpdateButton function is called before the change takes effect. This is because the task.wait(3) delay is causing the function to yield, and during that time, the other events might have occurred.

Modified script:

local OnTeleportation = false
local debounce = false

local function UpdateButton()
    print(OnTeleportation)
end

PlayButton.MouseButton1Click:Connect(function()
    if not debounce then
        debounce = true

        if not OnMatchmaking then
            OnMatchmaking = true

            task.spawn(function()
                UpdateButton()
                task.wait(3)

                Matchmaking:FireServer(OnMatchmaking)

                repeat task.wait() until QueuePlayers == 2 or not OnMatchmaking

                if QueuePlayers == 2 then
                    OnTeleportation = true
                    task.spawn(UpdateButton)
                    Matchmaking:FireServer(OnMatchmaking, "Teleportation")
                end
            end)
        else
            OnMatchmaking = false

            Matchmaking:FireServer(OnMatchmaking)

            MessageHandler.IssueMessage("Matchmaking canceled.", 2)

            task.spawn(function()
                for i = 3, 0, -1 do
                    PlayButton.Text = i.."s"
                    task.wait(1)
                end

                PlayButton.Text = "Play"
                debounce = false
            end)
        end
    end
end)

I replaced the direct call to UpdateButton with task.spawn(UpdateButton) after setting OnTeleportation to true. This should ensure that the update is performed after the yielding and the value of OnTeleportation has been properly set.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.