Spectate system not removing player from table properly

Hello. I am trying to make a spectate system that stops spectating a certain player if they have died. I am having some trouble making it so a remote event fires from the serverside and connects it to the client which removes the player from a table on the client side. I do not really understand how to get over this and I have looked up multiple solutions to which have failed. The remote event does fire but does not remove the player as intended.

Local script

diedRemote.OnClientEvent:Connect(function()
	
	
	wait(1)
	for i, plr in pairs(game.Players:GetPlayers()) do
		
	for i = 1, #plrs do
		if plrs[i] == plr then
			print(plrs[i])
			table.remove(plrs, i)
				print(plrs[i])
			else
				print("no")
			end
		end
	end
end)

I can send the full spectate script if needed.

you gotta watch out, there are two i's one is the in pairs i and the other i is from the for loop

3 Likes

This is a frequent oversight made by developers, this is why it’s a good practice to name your unused indices “_” (as overriding doesn’t matter) and your used indices; i1, i2, i3… or; Index1, Index2, Index3… (as overriding does matter).

The script works now. Had to insert an objectvalue to all the players when the round starts and keep checking whether the player has the value and if they do not, the script will remove the player from the table. I appreciate the help.

Don’t know if this would be considered “dumb” but…

-- Construct a table of players in the current instance
local players = game:GetService("Players"):GetPlayers()
-- Assuming plr is the LocalPlayer, find the position which their
-- player reference lies at and remove it.
table.remove(players, table.find(players, plr))
1 Like

Yes, that is about what I did lol. I have already found out. Thanks though