Tween camera back to the player when they die

I want to make it so that when the player dies, the camera tweens back to the player. I have good experience with tweening and camera manipulation, but I believe what I’m trying to achieve is too complex for me. Any ideas? If you have done it before, that’s perfect.

If you don’t know what I mean, here’s me demonstrating how the camera goes back to the player by default:

You could just have a script inside of StarterPlayerScripts (not StarterCharacter) to check when you die, lock the camera, and then tween it to your position when you respawn.

The problem is… your position is where you’ve locked the camera. I’m not sure how to get the new position anyway.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid: Humanoid | Instance? = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid",25)
local Camera = workspace.CurrentCamera
local Tween = game:GetService("TweenService")
Humanoid.Died:Connect(function()
    Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CameraSubject = Character:FindFirstChild("Torso")
end)
Player.CharacterAdded:Connect(function(Character)
	Tween:Create(Camera, TweenInfo.new(4), {CFrame = Character.HumanoidRootPart.CFrame}):Play()
	task.wait(4)
	Camera.CameraSubject = Humanoid
    Camera.CameraType = Enum.CameraType.Custom
end)

This should be in StarterPlayer.

2 Likes

First you need to create a Tweenservice, then add a connection to detect when the player respawns, and finally tween the players camera to the current (new) character.

This script by weakroblox35 says it all:

I would personally add more to the TweenInfo to make it more smooth (TweenInfo.new(4,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut))

My fault for not being specific. The origin should be where the character has died. Then I want the camera to tween back to the player.

Have you tested my snippet? If the result doesn’t come out as you expected it to be, let me know!

Yes, I did. But the tween starts when the character respawns. Maybe I should ask how to calculate the camera’s position without the game setting it.

Isn’t that what you wanted? Because I am confused.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Camera = workspace.CurrentCamera
local Tween = game:GetService("TweenService")

Player.CharacterAdded:Connect(function(Character)
	local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid",25)
	Humanoid.Died:Connect(function()
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CameraSubject = Character:FindFirstChild("Torso")
	end)
	if Camera.CameraType == Enum.CameraType.Scriptable then
		Tween:Create(Camera, TweenInfo.new(4), {CFrame = Character.HumanoidRootPart.CFrame}):Play()
		task.wait(4)
		Camera.CameraSubject = Humanoid
		Camera.CameraType = Enum.CameraType.Custom
	end
end)

This should fix it.

2 Likes

I thought I made it clear.

The script works really well. So now I have to figure out how to get the camera’s new position before the game sets it. No other action is needed. Thank you for helping! :smiley:

I’m now searching for where the camera CFrame is being set. It’s somewhere around the camera modules in the player.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.