CameraSubject refuses to change players

I’m thinking about giving up I seriously can’t figure out why the CameraSubject wont change

I’m trying to get it to Spectate another player, but it doesn’t go anywhere


local players = game:GetService("Players")
local spectateframe = script.Parent:WaitForChild("spectateframe")
local nextbutton = spectateframe:WaitForChild("next")
local lastbutton = spectateframe:WaitForChild("last")
local exitbutton = spectateframe:WaitForChild("exit")
local clickbutton = script.Parent:WaitForChild("spectatebutton")
local spectatelabel = spectateframe:WaitForChild("playerspectating")
local currentcamera = workspace.CurrentCamera
local char = players.LocalPlayer.Character or players.LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local isSpectating = false
local currentIndex = 1
local playerList = {}

local function UpdatePlayers()
	local newplayerlist = {}
	for i, plr in pairs(game.ReplicatedStorage.players:GetChildren()) do
		if plr.Name ~= game.Players.LocalPlayer.Name then
			print("working")
			table.insert(newplayerlist, plr.Name)
		end
	end
	playerList = newplayerlist
end

local function UpdateSpectator()
	
	local foundPlayer = players:FindFirstChild(playerList[currentIndex])
	
	if foundPlayer then
		print("found player")
		spectatelabel.Text = "Spectating: "..foundPlayer.Name
		print("switching camerasubject")
		currentcamera.CameraType = Enum.CameraType.Watch
		currentcamera.CameraSubject = foundPlayer.Character
		print(foundPlayer)
		print(foundPlayer.Character)
		print(currentcamera.CameraSubject)
		
	end
	UpdatePlayers()
end

local function NextPlayer()
	
	if currentIndex < #playerList then
		currentIndex += 1
	else
		currentIndex = 1
	end
	UpdateSpectator()
end

local function LastPlayer()
	
	if currentIndex > 1 then
		currentIndex -=1 
	else
		currentIndex = #playerList
	end
	UpdateSpectator()
end

local function QuitSpectate()
	
	isSpectating = false
	currentcamera.CameraSubject = hum
	
	currentIndex = 1
	spectateframe.Visible = false
end

clickbutton.MouseButton1Click:Connect(function()
	if game.ReplicatedStorage.roundstarted.Value == true then
	isSpectating = true
	
	spectateframe.Visible = isSpectating
	
	UpdatePlayers()
	UpdateSpectator()
	end
end)

nextbutton.MouseButton1Click:Connect(NextPlayer)
lastbutton.MouseButton1Click:Connect(LastPlayer)
exitbutton.MouseButton1Click:Connect(QuitSpectate)

It works but doesnt when the player dies

where is this script stored btw? i.e. which folder (StarterCharacterScripts, StarterPlayerScripts, some GUI?)

Its stored in a gui for spectating

just read your last reply, nvm

Its ok thank you for trying to help

camera is destroyed when the player respawns. I noticed the reference to the camera is defined at the top across the entire scripts scope. so once you reset, it keeps the old camera reference, but that camera is nil so it won’t work. define the camera variable in each function instead to fix it so you’re getting the current camera

This unfortunately doesnt work, I think I’m just a lost cause, thank you for trying to help though !