I think my table isn't being cleared in the first round -- getting a win even if dead in second round

Hey so i have 10 rounds ( = 1 game ) and i made a 2nd game because the game wasn’t timed perfectly.

  • first game has a timer of 60 seconds
  • second game has a timer of 48

I inserted a table where if a player dies they are removed and at the end of the 10th round, whoever is left in the table gets the win.

So for the first game (10 rounds) it works, if i stay alive all 10, i win, if not i don’t get a win

Then when i move to the second game, even if i die i get the win in leaderstats which is bizarre

My theory is that the table PlayersInServer isn’t getting cleared at the end of the first set of rounds/

here is the script of the first 2 games.

-- GAME 1 --


local RS = game:GetService("ReplicatedStorage")
local Colour = RS:WaitForChild("Colour")
local PlayersService = game:GetService("Players")
local PlayersInServer = PlayersService:GetPlayers()
local StarterBlock = workspace:WaitForChild("StarterBlock")
local CircleFolder = workspace:WaitForChild("Circles")

local PlrAddConn, PlrRemoveConn, CharAddConn, CharRemoveConn, DiedConn = nil, nil, nil, nil, nil
PlrConn = PlayersService.PlayerAdded:Connect(function(Player)
	table.insert(PlayersInServer, Player)
	if PlrRemoveConn then
		PlrRemoveConn:Disconnect()
	end
	CharAddConn = Player.CharacterAdded:Connect(function(Character)
		if DiedConn then
			DiedConn:Disconnect()
		end
		if CharRemoveConn then
			CharRemoveConn:Disconnect()
		end
		local Humanoid = Character:WaitForChild("Humanoid")
		DiedConn = Humanoid.Died:Connect(function()
			if table.find(PlayersInServer, Player) then
				table.remove(PlayersInServer, table.find(PlayersInServer, Player))
			end
		end)
	end)

	local CharRemoveConn = Player.CharacterRemoving:Connect(function(Character)
		if CharAddConn then
			CharAddConn:Disconnect()
		end
	end)
end)

local Timer = 60
for i = Timer, 0, -1 do
	task.wait(1)
	print(i)
end	

StarterBlock.Transparency = 0
StarterBlock.CanCollide = true

task.wait(2)

for i = 1, 3 do
	local Circles = CircleFolder:GetChildren() 
	Chosen_Colour = Circles[math.random(1, #Circles)]
	Colour.Value = Chosen_Colour.Name
	task.wait(6)
	StarterBlock.Transparency = 1
	StarterBlock.CanCollide = false 
	task.wait(2)
	for number,Circle_Colours in pairs(Circles) do
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			Circle_Part.Transparency = 1 
			Circle_Part.CanCollide = false
		end
	end

	for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
		Circle_Part.Transparency = 0 
		Circle_Part.CanCollide = true
	end

	task.wait(3)
	for number,Circle_Colours in pairs(Circles) do 
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			Circle_Part.Transparency = 0
			Circle_Part.CanCollide = true
		end
	end
end

for i=1,2 do
	Circles = CircleFolder:GetChildren() 
	Chosen_Colour = Circles[math.random(1,#Circles)]
	Colour.Value = Chosen_Colour.Name 
	task.wait(5)
	for number,Circle_Colours in pairs(Circles) do
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			Circle_Part.Transparency = 1 
			Circle_Part.CanCollide = false
		end
	end
	for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
		Circle_Part.Transparency = 0 
		Circle_Part.CanCollide = true
	end
	task.wait(3) 
	for number,Circle_Colours in pairs(Circles) do 
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			print("h")
			Circle_Part.Transparency = 0
			Circle_Part.CanCollide = true
		end
	end
end

for i=1,3 do
	Circles = CircleFolder:GetChildren() 
	Chosen_Colour = Circles[math.random(1,#Circles)]
	Colour.Value = Chosen_Colour.Name 
	task.wait(3)
	-- Round 6,7,8
	for number,Circle_Colours in pairs(Circles) do
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			Circle_Part.Transparency = 1 
			Circle_Part.CanCollide = false
		end
	end
	for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
		Circle_Part.Transparency = 0 
		Circle_Part.CanCollide = true
	end
	task.wait(3)
	for number,Circle_Colours in pairs(Circles) do 
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			print("h")
			Circle_Part.Transparency = 0
			Circle_Part.CanCollide = true
		end
	end
end

Circles = CircleFolder:GetChildren() 
Chosen_Colour = Circles[math.random(1,#Circles)]
Colour.Value = Chosen_Colour.Name 

task.wait(2)

for number,Circle_Colours in pairs(Circles) do
	for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
		Circle_Part.Transparency = 1 
		Circle_Part.CanCollide = false
	end
end

for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
	Circle_Part.Transparency = 0 
	Circle_Part.CanCollide = true
end

task.wait(3)

for number,Circle_Colours in pairs(Circles) do 
	for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
		print("h")
		Circle_Part.Transparency = 0
		Circle_Part.CanCollide = true
	end
end

task.wait(2)


StarterBlock.Transparency = 0
StarterBlock.CanCollide = true

Colour.Value = "Game ended."
for _, player in ipairs(PlayersInServer) do --change "PlayerList" to name of table alive players are stored inside
	local leaderstats = player:WaitForChild("leaderstats")
	if leaderstats then
		print("win was given")
		local Wins = leaderstats:WaitForChild("Wins")
		if Wins then
			Wins.Value += 1
		end
	end
end

task.wait(5)
Colour.Value = " "
PlayersInServer = {} -- clears table

---- REST OF GAMES -------


	local RS = game:GetService("ReplicatedStorage")
	local Colour = RS:WaitForChild("Colour")
	local PlayersService = game:GetService("Players")
	local PlayersInServer = PlayersService:GetPlayers()
	local StarterBlock = workspace:WaitForChild("StarterBlock")
	local CircleFolder = workspace:WaitForChild("Circles")

	local PlrAddConn, PlrRemoveConn, CharAddConn, CharRemoveConn, DiedConn = nil, nil, nil, nil, nil
	PlrConn = PlayersService.PlayerAdded:Connect(function(Player)
		table.insert(PlayersInServer, Player)
		if PlrRemoveConn then
			PlrRemoveConn:Disconnect()
		end
		CharAddConn = Player.CharacterAdded:Connect(function(Character)
			if DiedConn then
				DiedConn:Disconnect()
			end
			if CharRemoveConn then
				CharRemoveConn:Disconnect()
			end
			local Humanoid = Character:WaitForChild("Humanoid")
			DiedConn = Humanoid.Died:Connect(function()
				if table.find(PlayersInServer, Player) then
					table.remove(PlayersInServer, table.find(PlayersInServer, Player))
				end
			end)
		end)

		local CharRemoveConn = Player.CharacterRemoving:Connect(function(Character)
			if CharAddConn then
				CharAddConn:Disconnect()
			end
		end)
	end)

	local Timer = 48
	for i = Timer, 0, -1 do
		task.wait(1)
		print(i)
	end	

	StarterBlock.Transparency = 0
	StarterBlock.CanCollide = true

	task.wait(2)

	for i = 1, 3 do
		local Circles = CircleFolder:GetChildren() 
		Chosen_Colour = Circles[math.random(1, #Circles)]
		Colour.Value = Chosen_Colour.Name
		task.wait(6)
		StarterBlock.Transparency = 1
		StarterBlock.CanCollide = false 
		task.wait(2)
		for number,Circle_Colours in pairs(Circles) do
			for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
				Circle_Part.Transparency = 1 
				Circle_Part.CanCollide = false
			end
		end

		for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
			Circle_Part.Transparency = 0 
			Circle_Part.CanCollide = true
		end

		task.wait(3)
		for number,Circle_Colours in pairs(Circles) do 
			for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
				Circle_Part.Transparency = 0
				Circle_Part.CanCollide = true
			end
		end
	end

	for i=1,2 do
		Circles = CircleFolder:GetChildren() 
		Chosen_Colour = Circles[math.random(1,#Circles)]
		Colour.Value = Chosen_Colour.Name 
		task.wait(5)
		for number,Circle_Colours in pairs(Circles) do
			for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
				Circle_Part.Transparency = 1 
				Circle_Part.CanCollide = false
			end
		end
		for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
			Circle_Part.Transparency = 0 
			Circle_Part.CanCollide = true
		end
		task.wait(3) 
		for number,Circle_Colours in pairs(Circles) do 
			for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
				print("h")
				Circle_Part.Transparency = 0
				Circle_Part.CanCollide = true
			end
		end
	end

	for i=1,3 do
		Circles = CircleFolder:GetChildren() 
		Chosen_Colour = Circles[math.random(1,#Circles)]
		Colour.Value = Chosen_Colour.Name 
		task.wait(3)
		-- Round 6,7,8
		for number,Circle_Colours in pairs(Circles) do
			for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
				Circle_Part.Transparency = 1 
				Circle_Part.CanCollide = false
			end
		end
		for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
			Circle_Part.Transparency = 0 
			Circle_Part.CanCollide = true
		end
		task.wait(3)
		for number,Circle_Colours in pairs(Circles) do 
			for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
				print("h")
				Circle_Part.Transparency = 0
				Circle_Part.CanCollide = true
			end
		end
	end

	Circles = CircleFolder:GetChildren() 
	Chosen_Colour = Circles[math.random(1,#Circles)]
	Colour.Value = Chosen_Colour.Name 

	task.wait(2)

	for number,Circle_Colours in pairs(Circles) do
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			Circle_Part.Transparency = 1 
			Circle_Part.CanCollide = false
		end
	end

	for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
		Circle_Part.Transparency = 0 
		Circle_Part.CanCollide = true
	end

	task.wait(3)

	for number,Circle_Colours in pairs(Circles) do 
		for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
			print("h")
			Circle_Part.Transparency = 0
			Circle_Part.CanCollide = true
		end
	end

	task.wait(2)




	StarterBlock.Transparency = 0
	StarterBlock.CanCollide = true

	for _, player in ipairs(PlayersInServer) do --change "PlayerList" to name of table alive players are stored inside
		local leaderstats = player:WaitForChild("leaderstats")
		if leaderstats then
			print("win was given")
			local Wins = leaderstats:WaitForChild("Wins")
			if Wins then
				Wins.Value += 1
			end
		end
	end
	Colour.Value = "Game ended."
	task.wait(5)
	Colour.Value = " "
	PlayersInServer = {} -- clears table