Setting CameraType to Custom Doesn't Reset The Camera

I’m working on a respawn menu that upon player death, sets their camera’s cframe to a part to look at a scene. Once they hit the respawn button, their camera should then reset to the player. Unfortunately this works precisely once, and then afterwards it sets the camera’s position to the location where the player last died.

Here is my script (inside of a ScreenGui inside of StarterGui)

local WeaponSelectFrame = script.Parent.WeaponSelectFrame
local RespawnScreenFrame = script.Parent.RespawnScreenFrame

local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local respawnButton = RespawnScreenFrame.RespawnButton
local weaponSelectButton = RespawnScreenFrame.WeaponSelectButton

function setCamera()
	game.Workspace.Camera.CameraType = Enum.CameraType.Scriptable
	game.Workspace.CurrentCamera.CFrame = game.Workspace:WaitForChild("CameraPart").CFrame
	print("setting player's camera")
	RespawnScreenFrame.Visible = true

end

setCamera()

respawnButton.Activated:Connect(function()
	RespawnScreenFrame.Visible = false
	WeaponSelectFrame.Visible = false
	print("resetting camera")
	workspace.Camera.CameraType = Enum.CameraType.Custom
end)

hum.died:Connect(function()
	setCamera()
end)

You need your code to run for every added player.

Each time the character respawns, it gets a new humanoid. This means that doing

hum.died:Connect(function()
	setCamera()
end)

will only work for the first character and not the following respawned characters.


You can have code run for every character using Player.CharacterAdded:

local function onCharacterAdded(character)
    -- TODO: Get humanoid, create .Died:Once(function() end) connection, etc
end

local players = game:GetService("Players")
local player = players.LocalPlayer
-- Make sure to run for a potential already existing character
if player.Character then
    onCharacterAdded(player.Character)
end
-- Run for any additional characters
player.CharacterAdded:Connect(onCharacterAdded)

What I did (and I could definitely be wrong) was when I wanted to set the camera back to default, I would just set the Camera.Subject to nil