Saving Kills When Team Changes

I have a round system script that updates the players on the team’s kills and the current time. Whenever a player dies, they get moved to the Homescreen team.

I know the cause of why this happens. It’s because every second, the kill count variable (one for both teams) is reset to 0, and then it loops through every player on both teams and adds their number of kills to the variable. But if a player is now on the Homescreen team, the team will lose the amount of kills that that player had.

Here’s the code (the problem starts where it says “-- Problem Start”):

while true do
	-- Intermission
	
	local killsRed = 0
	local killsBlue = 0
	local winningTeam
	
	for i, plr in pairs(game.Players:GetChildren()) do
		if plr.Team == game.Teams:WaitForChild('Red') then
			killsRed += plr:WaitForChild('leaderstats'):WaitForChild('Kills').Value
		elseif plr.Team == game.Teams:WaitForChild('Blue') then
			killsBlue += plr:WaitForChild('leaderstats'):WaitForChild('Kills').Value
		end
	end
	
	if killsRed > killsBlue then
		winningTeam = game.Teams.Red
	elseif killsRed < killsBlue then
		winningTeam = game.Teams.Blue
	elseif killsRed == killsBlue then
		winningTeam = nil
	end
	
	for i, plr in pairs(game.Players:GetChildren()) do
		plr.Team = game.Teams.Homescreen
		plr:LoadCharacter()
		
		if winningTeam and plr.Team == winningTeam then
			plr.leaderstats.Kills.Value = 0
			plr.leaderstats.Wins.Value += 1
		end
	end
	
	game.ReplicatedStorage.RoundPlaying.Value = false
	game.ReplicatedStorage.WinningTeam:FireAllClients(winningTeam)
	
	task.wait(game.ReplicatedStorage.RoundPlaying.IntermissionTime.Value)
	
	-- Round
	
	game.ReplicatedStorage.RoundPlaying.Value = true
	
        -- Problem Start

	for i = game.ReplicatedStorage.RoundPlaying.RoundTime.Value, 0, -1 do
		local min = math.floor(i / 60)
		local sec = i % 60
		
		task.wait(1)
		
		killsRed = 0
		killsBlue = 0
		
		for i, plr in pairs(game.Players:GetChildren()) do
			if plr.Team == game.Teams:WaitForChild('Red') then
				killsRed += plr:WaitForChild('leaderstats'):WaitForChild('Kills').Value
			elseif plr.Team == game.Teams:WaitForChild('Blue') then
				killsBlue += plr:WaitForChild('leaderstats'):WaitForChild('Kills').Value
			end
		end
		
		if sec >= 10 then
			game.ReplicatedStorage.UpdateTime:FireAllClients(tostring(min).. ':'.. tostring(sec), killsRed, killsBlue)
		else
			game.ReplicatedStorage.UpdateTime:FireAllClients(tostring(min).. ':0'.. tostring(sec), killsRed, killsBlue)
		end
	end

        -- Problem End
end

I tried moving killsRed = 0 and killsBlue = 0 before the for loop, but then the one of the player’s kills keep racking up each second.

Thanks! All help appreciated! :slight_smile: