Camera script doesn't work when Humanoid dies

I’m trying to simulate a bird’s eye-view camera on my game, but the problem is that when the character dies (e.g. when the player resets it), the camera locks on the last character’s position before he died and when he respawns, it stays there, not moving at all.

Here’s the script I’m using, located in StarterPlayerScripts:

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer 
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild('HumanoidRootPart') 
local camera = workspace.CurrentCamera 
camera.CameraType = Enum.CameraType.Scriptable 

RunService.RenderStepped:Connect(function() 
if not root then return end 
     camera.CFrame = CFrame.new(root.CFrame.X , root.CFrame.Y + 30, root.CFrame.Z + 5) * CFrame.Angles(math.rad(-80), 0, 0) 
end)

If you have a solution, please let me know!

1 Like

This is because you are using the old character’s variables.

2 Likes

You have to set the CameraSubject to the players Humanoid

1 Like

Oh ok, I’ll follow this logic.

1 Like

Also, you can just put this inside starter character scripts and do

local character = script.Parent
1 Like

When you declare local root = character:WaitForChild('HumanoidRootPart') , Lua stores a reference to the root part once and never checks for a new one; your variable root only pertains to the first spawning of the character’s root. A really simple way to fix this is by placing your script in StarterCharacterScripts so that it refreshes upon respawn automatically.

Quick edit: If you do this, you can simply reference the character through the script’s parent since the script is replicated within the character model for every spawn.

3 Likes

It worked, thanks for the explanation!

1 Like