How would I change camera to a humanoid again if they die in my Spectate script

I have a spectate script, but I’m facing an issue to script it so - when a player dies, it automatically changes the camera to the player when they respawn. Right now, if someone dies, the camera is stuck at that place forever.

local cam = game.Workspace.CurrentCamera

local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.Button

function get()
	for _,v in pairs(game.Players:GetPlayers())do
		if v.Name == title.Text then
			return(_)
		end
	end
end


local debounce = false

button.MouseButton1Click:connect(function()
	if debounce == false then 
		debounce = true
		bar:TweenPosition(UDim2.new(0,0,0,0),"In","Linear",1,true)
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	elseif debounce == true then debounce = false
		pcall(function() cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid end)
		bar:TweenPosition(UDim2.new(1,0,0,0),"In","Linear",1,true)
	end
end)

prev.MouseButton1Click:connect(function()
	wait(.1)
	local players = game.Players:GetPlayers()
	local num = get()
	if not pcall(function() 
			cam.CameraSubject = players[num-1].Character.Humanoid
		end) then
		cam.CameraSubject = players[#players].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

nex.MouseButton1Click:connect(function()
	wait(.1)
	local players = game.Players:GetPlayers()
	local num = get()
	if not pcall(function() 
			cam.CameraSubject = players[num+1].Character.Humanoid
		end) then
		cam.CameraSubject = players[1].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:lower() == "e" or key:upper() == "E" then
		if debounce == true then
			
			wait(.1)
			local players = game.Players:GetPlayers()
			local num = get()
			if not pcall(function() 
					cam.CameraSubject = players[num+1].Character.Humanoid
				end) then
				cam.CameraSubject = players[1].Character.Humanoid
			end
			pcall(function()
				title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
			end)
		end
	end
end)

mouse.KeyDown:connect(function(key)
	if key:lower() == "q" or key:upper() == "Q" then
		if debounce == true then
			wait(.1)
			local players = game.Players:GetPlayers()
			local num = get()
			if not pcall(function() 
					cam.CameraSubject = players[num-1].Character.Humanoid
				end) then
				cam.CameraSubject = players[#players].Character.Humanoid
			end
			pcall(function()
				title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
			end)
		end
	end
end)
1 Like

Connect a humanoid.Died event to a function to see when they die, and when they do, change it over to another person. also, make a variable for the connection, so that if you switch over to another player, you can disconnect the old event and connect a new one, so that you save performance.

Example:

local diedConnection

function onPlayerDied()
   diedConnection:Disconnect()
   --do stuff to find new player
end
function setCameraSubject(player)
   --Do the other stuff
   diedConnection = player.Character.Humanoid.Died:Connect(onPlayerDied)
end

Or just make the camera custom again when they die

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(c)
c:FindFirstChild(“Humanoid”).Died:Connect(function()
workspace.CurrentCamera.Camera.CameraType = Enum.CameraType.Custom
end)
end)
end)
1 Like

Add this to the very bottom of the script.

game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
    local humanoid = character.Humanoid
    humanoid.Died:Connect(function()
        workspace.CurrentCamera.Camera.CameraType = Enum.CameraType.Custom
        workspace.CurrentCamera.CameraSubject = humanoid
    end)
end)

I tried all the codes above and none of them worked.

Maybe try to do a humanoid.Died and wait and then set the camera back to the same person?

I’d suggest making a part that is invisible and is cancollide false right where the player spawns, then script it in a way so that when the player touches the part on respawn it’ll just set the camera onto it