Weird Round Behavior

Heya there!

I’ve been working on a round system for a commission, it’s basically like Street Fighter.

There are 2 rounds (3 if needed, I’ll explain this in a bit) in a match. If you kill the enemy in 2 rounds, you win. But if you and the enemy both get 1 win (tie), there will be a 3rd round (sudden death) and first to hit the opposing player wins.

For some reason, the script doesn’t print out that a player has won a round whenever you win (2 - 0) or win sudden death (2 - 1). Weird thing is, it works just fine whenever the player is about to get a tie (before the round the enemy gets 1 win, then he is about to get 1 win the next round)

I’m very much lost, not sure what the problem is.

I handle the win conditions inside StartMatch().

local PS = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local suddenDeath = RS.Events.SuddenDeath
local timeSync = RS.Events.Timer
local wins = {}
local started = false
local ended = false
local config = {
	timeLimit = 180,
	intermission = 3
}

repeat task.wait() until #PS:GetPlayers() > 1

local player1: Player, player2: Player = PS:GetPlayers()[1], PS:GetPlayers()[2]

local function YieldUntilEnd(): Player?
	local bindable = Instance.new("BindableEvent")
	local winner = nil
	local con = {}
	
	task.delay(config.timeLimit, function()
		bindable:Fire()
	end)
	task.spawn(function()
		for _,v: Player in ipairs(PS:GetPlayers()) do
			local char: Model = v.Character or v.CharacterAdded:Wait()
			local hum: Humanoid = char and char:FindFirstChildWhichIsA("Humanoid")
			
			
			hum.Died:Connect(function()
				winner = if v ~= player1 then player1 else player2
				bindable:Fire()
				warn("SOMEBODY DIED")
			end)
		end
	end)
	
	bindable.Event:Wait()
	bindable:Destroy()
	
	return winner
end

local function AddWinToPlayer(player: Player)
	if wins[player] then
		wins[player] += 1
	else
		wins[player] = 1
	end
end

local function ClientTimer(num: number)
	if not started then
		return
	end
	
	task.spawn(function()
		for i = num,0,-1 do
			if not started then
				break
			end

			timeSync:FireAllClients(i)
			task.wait(1)
		end
	end)
end

local function StartMatch()
	warn("intermission")
	task.wait(config.intermission)
	timeSync:FireAllClients(config.timeLimit)

	warn("started")
	started = true
	
	ClientTimer(config.timeLimit)
	local winner = YieldUntilEnd()
	
	if winner then
		local opposing = if winner ~= player1 then player1 else player2
		AddWinToPlayer(winner)
		warn(tostring(wins[winner]) .. " -> " .. tostring(wins[opposing]))

		if wins[winner] == 2 and wins[opposing] == nil or wins[winner] == 2 and wins[opposing] == 1 then
			warn(winner.Name .. " has won!")
			ended = true
		elseif wins[winner] == 1 and wins[opposing] == 1 then
			warn("TIE")
			suddenDeath:Fire()
		else
			warn(winner.Name .. " has succeeded to next match!, how many wins? " .. wins[winner])
			warn(opposing.Name .. " has lost!")
		end
	else
		warn("noooo time limit...")
	end
	started = false
end

while true do
	if ended then
		break
	end
	
	if not started then
		StartMatch()
	end
	task.wait()
end

Weirdly enough, for the conditions that I mentioned above that are broken, they don’t even print out “SOMEBODY DIED” which I put inside YieldUntilEnd()?

Fixed, apparently it was running the YieldUntilEnd() function while the player was still dead.