Why are my functions not disconnecting properly

Ok so I have a couple functions that I add into a table and then disconnect when a condition is met. However, once the condition is met, the function runs one more time before actually disconnecting… Any ideas why?

local function disconnectConnections(wins)
		if wins == 5 then
			for i, v in ipairs(connections) do
				v:Disconnect()	
			end

			table.clear(connections)
local function checkDeaths()
		-- Checks that one whole team is dead
		if T1Player1Dead == true and T1Player2Dead == true and T1Player3Dead == true then
			team2Wins = team2Wins + 1
			
			-- Updates scoreboard
			workspace:FindFirstChild("Sword Fighting Arenas").Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = team1Wins
			workspace:FindFirstChild("Sword Fighting Arenas").Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = team2Wins
			workspace:FindFirstChild("Sword Fighting Arenas").Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = team1Wins
			workspace:FindFirstChild("Sword Fighting Arenas").Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = team2Wins
			
			-- Checks if the team has reached 5 wins
			if disconnectConnections(team2Wins) then
				return -- THIS SHOULD STOP THE FUNCTION FROM CONTINUING SINCE THE CRITERIA HAS BEEN MET BUT IT FINISHES RUNNING THIS FUNCTION??
			else
				task.wait(3)

				-- Making sure everyone is full health before the next round
				for i = #team2, 1, -1 do
					team2[i].Character.Humanoid.Health = 100
				end

Try this:

-- Assuming 'connections' is a table that holds all the connections you want to disconnect
local connections = {}

-- Function to disconnect all connections
local function disconnectConnections(wins)
    if wins == 5 then
        for i, v in ipairs(connections) do
            v:Disconnect()
        end
        table.clear(connections)
        return true
    else
        return false
    end
end

-- Function to check if a team has won
local function checkWins(teamWins)
    if teamWins == 5 then
        -- Disconnect connections if a team has 5 wins
        return disconnectConnections(teamWins)
    end
    return false
end

-- Function to check deaths and update wins
local function checkDeaths()
    -- Checks that one whole team is dead
    if T1Player1Dead and T1Player2Dead and T1Player3Dead then
        team2Wins = team2Wins + 1
        
        -- Updates scoreboard
        -- ... (rest of your scoreboard update code)

        -- Checks if the team has reached 5 wins
        if checkWins(team2Wins) then
            return -- Stops the function from continuing
        end

        task.wait(3)
        -- Making sure everyone is full health before the next round
        -- ... (rest of your health reset code)
    end
end

-- Example of how you might add a connection to the 'connections' table
local someConnection = game.SomeEvent:Connect(function()
    -- Some event logic
end)
table.insert(connections, someConnection)

-- Example of how you might call 'checkDeaths' when needed
-- checkDeaths() -- Uncomment and place this where you need to call the function

1 Like

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