Not able to remove players from a currentplayer table

hello all! So I have a script set up, and when a player dies it calls this function (player.Character.removing(removeplayers) and it works somewhat, the outputs are all called like the print statements, with the correct name, however the players position is not changed and doesn’t call an error, and the wins are always given to the same player even though the table should only contain the only player remaining.

local removeplayers = function (player)
	print(player.Name.." died")
	table.remove(currentPlayers, table.find(currentPlayers, player))
		if #currentPlayers <= 1 then
		inGame.Value = false
		player.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame +,5,0)
		currentPlayers[1].leaderstats.Wins.Value = currentPlayers[1].leaderstats.Wins.Value + 1

I also have the code below which is just in the middle of my game loop basically.

for i,v in pairs(Players:GetChildren()) do
				v.Character.HumanoidRootPart.CFrame = game.Workspace.startPos.CFrame

but the problem here seems to come from the table being really wierd. I’m not even sure how it works I’ve done plenty of testing, in the first code block I have shown it prints the player who died easily, however when it comes to printing the 1st value in the table it calls an error. and when testing, it doesn’t matter who dies, who is or isn’t in the table, player 1 or in other cases the first person to join always gets the win added to their stat. why is this? any help is appreciated, I am at a total roadblock here

I believe this is because the winner is kept inside of the table, and each time adds them to the table again, causing them to always win.

after setting currentPlayers = {},
the following still happened to occur, and it didn’t matter who died, when they died, etc. player 1 always got a win whether they where in the table or no?

I assume these are lines 24-27. In this segment, you set up a condition that is true when there are no longer any objects in the table. You then try to get the name of the first object of the table in line 27. It’s going to throw an error because there’s nothing in the table.

There’s nothing in the table because you have your event inside of the for loop. When the event is fired, the for loop activates again and performs removeplayers an amount of times equivalent to the amount of players in the currentPlayers table at the time the event was fired.

To demonstrate this, I put together this script. If you’d like to test this script, copy it and paste it inside of a local script, and place that local script inside of StarterPlayerScripts

Input = game:GetService("UserInputService")

local iterator = {1, 2, 3, 4, 5, 6, 7} -- table mimicking your player table

for i, _ in ipairs(iterator) do --your loop
	print("loop started")
	Input.InputBegan:Connect(function() --your event listener
		print("Loop began")

You’ll notice that on the first round through, the script works as expected and prints “Loop Started” 7 times. However, each time you give user input, the script will output “Loop Began” 7 times to the console because the size of the table being iterated through is 7.

I must also mention, you allow this function to execute when there are 0 Players in the table, meaning when you try to find the object at index 1, you’ll get nil.

Hey dude! thanks for all the help, I finally go it working. because I had it set to character removing, I was passing the character to the remove from table script, which then didn’t remove the player from table because i was actually trying to remove the character from the table, which obviously wasn’t there. This fixed all my problems. But i am extremely grateful for your efforts, I know going around helping people like me doesn’t pay you much haha but without people like you I would go no where. Thank you again!