Player that dies not being removed from table

I am making a round based survival game where the winners of the round get given points for surviving the round. The points system works fine in singleplayer, but when there are multiple players the player that dies is supposed to be removed from the table, instead the player that dies is the only player in the table and the only one to receive points.

ButtonPressed Script: (Server)

local Text = game.StarterGui.Folder.ScreenGui.Text
local TimeText = game.StarterGui.Folder.Timer.TimeText
local Status = game.ReplicatedStorage:WaitForChild("Status")
local Click = game.Workspace.Button.Button.ClickDetector
local Event = game.ReplicatedStorage.Events.ButtonPress
local RoundEnd = game.ReplicatedStorage.Events.RoundEnd
local button = game.Workspace.Button.Button
local debounce = false



	
	
	Click.MouseClick:Connect(function(player)
	Click.Parent = nil
	local Points = player.leaderstats:FindFirstChild("Points")
	if Points then 
		print("Found points")
	else
		print("Points not found")
	end
	Event:Fire() 
	print("d")
		if debounce == false then
		debounce = true
		button.BrickColor = BrickColor.new("Really red")
	end

	RoundEnd.Event:Connect(function()
		print("RoundEnd")
		Status.Value = "Survivors have been given Points!"
		wait(5)
		debounce = false
        Click.Parent = game.Workspace.Button.Button
		button.BrickColor = BrickColor.new("Lime green")
		Text.Text = "Press the button!"
		TimeText.Text = ""
	end)
	
end)				
        	

Event script:

local PlayerTable = {}
local Player = game.Players:GetPlayers()
local ButtonPress = game.ReplicatedStorage.Events.ButtonPress
local RoundEnd = game.ReplicatedStorage.Events.RoundEnd
local Text = game.StarterGui.Folder.ScreenGui.Text
local TimeText = game.StarterGui.Folder.Timer.TimeText
local Timer = game.ReplicatedStorage:WaitForChild("Timer")
local Status = game.ReplicatedStorage:WaitForChild("Status")
local chooseape = game.ReplicatedStorage.Maps:GetChildren()[math.random(1, #game.ReplicatedStorage.Maps:GetChildren())]
local TimeValue = chooseape.Configs.RoundTime.Value
local TelePlayer = chooseape.Configs.TeleportButtonPresser
script.Parent.ClickDetector.MouseClick:Connect(function(player)
	TelePlayer.Value = player.Name
end)






Text.Text = "Press the button!"
TimeText.Text = ""


ButtonPress.Event:Connect(function()
	for _, player in pairs((game.Players:GetPlayers())) do
		table.insert(PlayerTable, player)		
		player.Character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name.."has died!")
			table.remove(PlayerTable, table.find(PlayerTable, player.Name))
            print(PlayerTable)
		end)
	end

	chooseape:Clone().Parent = workspace
	
		for i = TimeValue, 0, -1 do
		Status.Value = chooseape.Name.."!"
		Timer.Value = i
		wait(1)
		
		end	
		print("test")
		TelePlayer.Value = ""
	RoundEnd:Fire()
	Status.Value = ""
	chooseape:Destroy()

	for i, player in pairs((PlayerTable)) do
		player.leaderstats.Points.Value = player.leaderstats.Points.Value + 10
	end
	
	Text.Text = "Press the button!"
	TimeText.Text = ""
end)

leaderstats

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Points = Instance.new("IntValue", leaderstats)
	Points.Name = "Points"
end)

Any help would be greatly appriciated!

3 Likes