When players die, they are not removed from table (so they get given a win)

Hey so I am making a color block game and I made a table with all players in the server and when a player dies in the rounds, they are removed from the table. The issue is when I tested the game, the player doesn’t get removed from the table and when the 10 rounds finish, the losers get the win aswell which isn’t normal.

--- GAME 1 -----



game.Players.PlayerAdded:Connect(function(player)
	local loaded = player:HasAppearanceLoaded()
	print(loaded)
	
	while not loaded do												--- Wait for player to load
		loaded = player:HasAppearanceLoaded()
		print(loaded)
		wait()
	end
end)

local PlayersInServer = game.Players:GetPlayers(); -- Table of players in server

local PlayersService = game:GetService("Players")
local Players = PlayersService:GetPlayers()
local PlrAddConn, PlrRemoveConn, CharAddConn, CharRemoveConn, DiedConn = nil, nil, nil, nil, nil

PlrConn = PlayersService.PlayerAdded:Connect(function(Player)
	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()
			table.remove(PlayersInServer, table.find(PlayersInServer, Player))
		end)
	end)
	local CharRemoveConn = Player.CharacterRemoving:Connect(function(Character)
		if CharAddConn then
			CharAddConn:Disconnect()
		end
	end)
end)

PlrRemoveConn = PlayersService.PlayerAdded:Connect(function(Player)
	if PlrConn then
		PlrConn:Disconnect()
	end
end)

	
	local Timer = 60

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



local StarterBlock = game.Workspace.StarterBlock

StarterBlock.Transparency = 0
StarterBlock.CanCollide = true

wait(2)

for i=1,3 do
	
	


		Circles = game.Workspace.Circles:GetChildren() 

		Chosen_Colour = Circles[math.random(1,#Circles)]

		game.ReplicatedStorage.Colour.Value = Chosen_Colour.Name    --- ROUND 1, 2 AND 3

		task.wait(6)

		StarterBlock.Transparency = 1
	StarterBlock.CanCollide = false 


	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

		wait(3) -- waits again

		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
	
				
					
	print("Round 1, 2 and 3 completed")





	for i=1,2 do

		Circles = game.Workspace.Circles:GetChildren() 

		Chosen_Colour = Circles[math.random(1,#Circles)]

		game.ReplicatedStorage.Colour.Value = Chosen_Colour.Name 

		task.wait(5)
																									-- ROUND 4,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

		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
								

	print("Round 4, 5 and 6 completed")






	for i=1,3 do

		Circles = game.Workspace.Circles:GetChildren() 

		Chosen_Colour = Circles[math.random(1,#Circles)]

		game.ReplicatedStorage.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

		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
									
	print("Round 6,7 and 8 completed")




		Circles = game.Workspace.Circles:GetChildren() 

		Chosen_Colour = Circles[math.random(1,#Circles)]

		game.ReplicatedStorage.Colour.Value = Chosen_Colour.Name 

		task.wait(2)
																						-- ROUND 9,10
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

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

wait(2)
print("Round 9 and 10 completed")


local StarterBlock = game.Workspace.StarterBlock


			StarterBlock.Transparency = 0
StarterBlock.CanCollide = true


local Players = game:GetService("Players")
local PlayersInServer = Players:GetPlayers()

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
		local Wins = leaderstats:WaitForChild("Wins")
		if Wins then
			Wins.Value += 1
		end
	end
end

game.ReplicatedStorage.Colour.Value = "Game ended."


wait(3.5)

game.ReplicatedStorage.Colour.Value = " "

PlayersInServer = {} -- clears table

PlayersInServer is the table your using to iterate through right? That’s everyone in the server so isn’t this loop just giving everyone a win?

You re-define “PlayersInServer” here with the current players in the server so it would overwrite any changes made to the previous table.

1 Like

Well i want to put all the players in a single table at the start of the game, when players die they are removed from the table and at the end of round 10, the remaining players in the table are given a win, the table then removes everything inside and the script starts again.

From what I can tell by skimming the script it looks like the original table is called “PlayersInServer” and the removal is handled by this line …

But later in the script just before the for loop assigning wins, you overwrite that original table with a current table of all players here.

Why are you handling the connections of server-controlled events? There’s no reason to do stuff like

PlrConn = Players.PlayerAdded:Connect(function(Player)

And there’s especially no reason to disconnect that event when a player leaves. It’s not local, so you’re disconnecting that event from running again? (Assumption, never done that). Connections per object are cleared automatically when they’re removed. So when the player leaves, all their connections automatically disconnect.

Also, during your round code you could define PlayersInServer as Players:GetPlayers() just as you did. Inside your PlayerAdded event, you can add a Player.CharacterAdded event. Make sure the Humanoid is there (:WaitForChild()) and then run the Humanoid.Died event. So when any player’s character dies, that event will fire. That function can look something like this.

local function OnPlayerAdded(Player)
    local function OnCharacterAdded(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
        if Humanoid then
            Humanoid.Died:Connect(function()
               if table.find(PlayersInServer,Player) then
                   table.remove(PlayersInServer,table.find(PlayersInServer,Player)
               end
            end
        end
    end
    if Player.Character then
        OnCharacterAdded(Player.Character)
    end
    Player.CharacterAdded:Connect(OnCharacterAdded)
end

local function OnPlayerRemoved(Player)
    if table.find(PlayersInServer,Player) then
        table.remove(PlayersInServer,table.find(PlayersInServer,Player)
    end
end

--// Events
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoved:Connect(OnPlayerRemoved)

This was made up on the spot so I’m not sure how refined it is, but that’s generally how I would do it.

table.remove(PlayersInServer, table.find(PlayersInServer, Player, Player.Name))

You also need to add the player’s name to the table, along with their name. The problem was that when table.remove() was called, inside table.find() you only referenced the player object, not the name.

You can’t pass 3 arguments to table.find() like that.

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)
	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

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
		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