Auto-win function not properly functioning

I am currently creating a game that is based off of minigame style rounds, and as a beta (the first round) I confronted a quite tricky problem that I am honestly not in the mood to lose sleep over, the function is below. I will include the problem after this.

function attemptBetaRoundStart()
	if playercount > 1 then
		local players = game:GetService("Players"):GetPlayers()
		local player1 = players[math.random(1, #players)]
		local player2 = players[math.random(1, #players)]
		if player2 == player1 then
			repeat
				wait()
				player2 = players[math.random(1, #players)]
			until player2 ~= player1
		end
		game.Players.PlayerRemoving:Connect(function(p)
			if p == player1 then
				publicannounce(player1.Name.." has disconnected, "..player2.Name.." wins by default.", 5)
				attemptBetaRoundStart()
			elseif p == player2 then
				publicannounce(player2.Name.." has disconnected, "..player1.Name.." wins by default.", 5)
				attemptBetaRoundStart()
			end
		end)
		publicannounce("The game is starting, a fight against "..player1.Name.." and "..player2.Name..".", 3)
		publicannounce("Your objective is to knock the other opponent off the edge into the lava, good luck.", 3)
		
		player1:LoadCharacter()
		player1.Character.Parent = game.Workspace.Alive
		player2:LoadCharacter()
		player2.Character.Parent = game.Workspace.Alive
		
		player1.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.spawn1.Position)
		player2.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.spawn2.Position)
		
		enableBall(player1.Name, player2.Name)
		
		repeat
			wait()
		until player1.Character.Humanoid.Health == 0 or player2.Character.Humanoid.Health == 0
		
		if player1.Character.Humanoid.Health > 0 then 
			publicannounce(player1.Name.." wins!", 3)
			player1:LoadCharacter()
			player2:LoadCharacter()
		elseif player2.Character.Humanoid.Health > 0 then
			publicannounce(player2.Name.." wins!", 3)
			player1:LoadCharacter()
			player2:LoadCharacter()
		end
		publicannounce("Starting new game...", 3)
		wait(1)
		attemptBetaRoundStart()
	else
		wait(1)
		attemptBetaRoundStart()
	end
end

Now, so the problem is that the connect function in line 12 will not work properly as I would like it to, and that is because when the next round comes after this auto win round, if the winner of such round disconnects while another round is going on, it would interrupt the round and end it instantly. If you could help me come up with a better way of doing this, I would be thankful.

Correct me if I’m wrong.

I assume you need to disconnect the connection after the player left the game.

local PlayerRemovingConnection

PlayerRemovingConnection = game.Players.PlayerRemoving:Connect(function(p)
	if p == player1 then
		PlayerRemovingConnection:Disconnect()
		publicannounce(player1.Name.." has disconnected, "..player2.Name.." wins by default.", 5)
		attemptBetaRoundStart()
	elseif p == player2 then
		PlayerRemovingConnection:Disconnect()
		publicannounce(player2.Name.." has disconnected, "..player1.Name.." wins by default.", 5)
		attemptBetaRoundStart()
	end
end)

That sounds to be exactly what I’d need, I’ll test it later and tell you if it works. Thank you.

It worked perfectly, thank you!

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