Problems with spectating system

Hello devs! I’m currently trying to achieve a spectating system, which when you die, the games calculates how much players are alive, if there’s atleast one player alive, the game will make the dead player spectate others alive player, but somehow, it keeps getting this error:

Here’s a fraction of my script:

local function onCharacterAdded(character)
	local plyr = game:GetService("Players").LocalPlayer -- This is how you could get the player here
local players
local plrNumber
local player = game:GetService("Players")

	local humanoid = character:WaitForChild("Humanoid", 5)
	if not humanoid then
		return
	end

	humanoid.Died:Connect(function()
		for i, v in pairs(game.Players:GetPlayers()) do
			task.wait(.1)
			players = v
			plrNumber = i
			end

if players.Character.Humanoid.Health > 0 and plrNumber > 1 then
				script["Static Effect"]:Stop()
				script["VHS tape clicks and rewind 1"]:Stop()
	plyr.PlayerGui.DeathGui.Spectate.Visible = true
	plyr.PlayerGui.DeathGui.MEGdeath.Visible = false
	local nextButton = plyr.PlayerGui.DeathGui.Spectate.Next
			local previousButton = plyr.PlayerGui.DeathGui.Spectate.Previous
			local playerLabel = plyr.PlayerGui.DeathGui.Spectate.NameLabel
			
			local cam = workspace.CurrentCamera
			local currentIndex = 1
			local playersList = {}
			
			
			local function updatePlayers()
				local newPlayersList = {}
				
				for plrs, plr in pairs(game.Players:GetPlayers()) do
					if plr ~= game.Players.LocalPlayer then
						table.insert(newPlayersList, plr.Name)
					end
				end
				playersList = newPlayersList
			end
			
			local function updateSpectator() 
				local foundPlayer = player:FindFirstChild(playersList[currentIndex])
				print(players, playersList[1], currentIndex)
				if foundPlayer then
					playerLabel.Text = foundPlayer.Name
					cam.CameraSubject = foundPlayer.Character
					game.ReplicatedStorage.Remotes.LoadChars:FireServer()
					print("FoundPlayers")
				else
					print("Not found plr")
				end
			end
			
			local function nextPlayer()
				if currentIndex < #playersList then
					currentIndex += 1
				else
					currentIndex = 1 
				end
				script["Button Click"]:Play()
				updatePlayers()
				task.wait()
				updateSpectator()
				print("NExt")
			end
			
			local function lastPlayer()
				if currentIndex > 1 then
					currentIndex -= 1
				else
					currentIndex = #playersList
				end
				script["Button Click"]:Play()
				updatePlayers()
				task.wait()
				updateSpectator()
				print("last")
			end
			

			
			nextButton.MouseButton1Click:Connect(nextPlayer)
			previousButton.MouseButton1Click:Connect(lastPlayer)
			
			updatePlayers()
			updateSpectator()

Just to remember, it’s a local script in stater gui