Help with script not working after death

hello, i have a vault script that works fine but when a character is spawned back after death/reset the scripts camera wont follow, any help?

local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Head = character:WaitForChild("Head")
local Humanoid = character:WaitForChild("Humanoid")

local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
Controls:Disable()
Controls:Enable(true)

local CameraModule = require(game:GetService("ReplicatedStorage"):WaitForChild("CameraModule"))

local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {player}

local Animator = Humanoid:WaitForChild("Animator")

local Ledge_JumpStart_Track = Animator:LoadAnimation(script:WaitForChild("Ledge_JumpStart"))
local Ledge_JumpMid_Track = Animator:LoadAnimation(script:WaitForChild("Ledge_JumpMid"))
local Ledge_ClimbUp_Track = Animator:LoadAnimation(script:WaitForChild("Ledge_ClimbUp"))
local Ledge_ClimbForwards_Track = Animator:LoadAnimation(script:WaitForChild("Ledge_ClimbForwards"))

Ledge_JumpStart_Track.Priority = Enum.AnimationPriority.Action
Ledge_JumpMid_Track.Priority = Enum.AnimationPriority.Action
Ledge_ClimbUp_Track.Priority = Enum.AnimationPriority.Action
Ledge_ClimbForwards_Track.Priority = Enum.AnimationPriority.Action

Ledge_JumpStart_Track:SetAttribute("ViewModelAnimation", true)
Ledge_JumpMid_Track:SetAttribute("ViewModelAnimation", true)
Ledge_ClimbUp_Track:SetAttribute("ViewModelAnimation", true)
Ledge_ClimbForwards_Track:SetAttribute("ViewModelAnimation", true)

local Halfwall_RunStart_Track = Animator:LoadAnimation(script:WaitForChild("Halfwall_RunStart"))
local Halfwall_VaultUp_Track = Animator:LoadAnimation(script:WaitForChild("Halfwall_VaultUp"))
local Halfwall_VaultForwards_Track = Animator:LoadAnimation(script:WaitForChild("Halfwall_VaultForwards"))
local Halfwall_VaultLand_Track = Animator:LoadAnimation(script:WaitForChild("Halfwall_VaultLand"))

Halfwall_RunStart_Track.Priority = Enum.AnimationPriority.Action
Halfwall_VaultUp_Track.Priority = Enum.AnimationPriority.Action
Halfwall_VaultForwards_Track.Priority = Enum.AnimationPriority.Action
Halfwall_VaultLand_Track.Priority = Enum.AnimationPriority.Action

Halfwall_RunStart_Track:SetAttribute("ViewModelAnimation", true)
Halfwall_VaultUp_Track:SetAttribute("ViewModelAnimation", true)
Halfwall_VaultForwards_Track:SetAttribute("ViewModelAnimation", true)
Halfwall_VaultLand_Track:SetAttribute("ViewModelAnimation", true)


local function AlignPositionSetUp()
	local AlignPosition = Instance.new("AlignPosition", HumanoidRootPart)
	AlignPosition.Enabled = false
	AlignPosition.Name = "AlignPosition"
	AlignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
	AlignPosition.Attachment0 = HumanoidRootPart.RootRigAttachment
	AlignPosition.MaxForce = 100000000
end
AlignPositionSetUp()

local function AlignOrientationSetup()
	local AlignOrientation = Instance.new("AlignOrientation", HumanoidRootPart)
	AlignOrientation.Enabled = false
	AlignOrientation.Name = "AlignOrientation"
	AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
	AlignOrientation.Attachment0 = HumanoidRootPart.RootRigAttachment
	AlignOrientation.PrimaryAxisOnly = true
	AlignOrientation.RigidityEnabled = true
end
AlignOrientationSetup()

local CurrentStamina = player.PlayerGui:WaitForChild("StaminaBarGui"):WaitForChild("SprintScript"):WaitForChild("CurrentStamina")
local StaminaCost = 40

local function DrainStamina()
	local finalStamina = CurrentStamina.Value - StaminaCost
	print(finalStamina)
	if not (finalStamina < 0) then
		CurrentStamina.Value = finalStamina
		return true
	else
		return false
	end
end

local isClimbing = false

local function ClimbCheck(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		
		if not isClimbing then
			isClimbing = true
			
			local raycastResult = workspace:Raycast(character.PrimaryPart.CFrame.Position - Vector3.new(0,1,0), character.PrimaryPart.CFrame.LookVector * 5  - Vector3.new(0,1,0), raycastParams)

			if raycastResult then
				if raycastResult.Instance ~= nil and raycastResult.Instance.Parent ~= nil and raycastResult.Position ~= nil then
					
					local HitPart = raycastResult.Instance
					local HitPosition = raycastResult.Position
					
					if HitPart:GetAttribute("CanClimb") == true then
						if (raycastResult.Position - character.HumanoidRootPart.Position).magnitude < 4 then
							if not DrainStamina() then print("NO STAMINA") isClimbing = false return end
								
								CameraModule.CameraMode("LockFirstPersonCamera")
							
							if HitPart.Size.Y > 6 then		-- Climb ledge
								
								HitPart.CanCollide = false
								Controls:Disable()
								player.PlayerGui.StaminaBarGui.SprintScript.CanCrouch.Value = false
								
								-- Initialise --
								local AlignPosition = HumanoidRootPart:WaitForChild("AlignPosition")	-- Get AlignPosition Constraint in HumanoidRootPart
								local AlignOrientation = HumanoidRootPart:WaitForChild("AlignOrientation")
			
								AlignOrientation.Enabled = false
								AlignOrientation.CFrame = CFrame.Angles(0,math.rad(HitPart.Orientation.Y),0)
								AlignOrientation.Enabled = true

								-- Ledge_JumpStart & Ledge_JumpMid --
								Ledge_JumpStart_Track:Play(0.1, 1, 0.6)			-- Play the starting portion of the jump animation
								task.wait(Ledge_JumpStart_Track.Length / 3)		-- Wait until the animation is almost finished but not fully finished
								Ledge_JumpMid_Track:Play(0.1, 1, 0.6)				-- Start playing the rest of the jump animation while the initial animation is almost done
								
								-- Get position of target destination
								local newPosition = HitPosition - (character.PrimaryPart.CFrame.LookVector * 1.3)	-- The position a few studs in front of the wall (X and Z coordinates)
								local newHeight = HitPart.Size.Y/2 + HitPart.Position.Y			-- The position at the top of the wall (Y coordinates)

								-- Start moving character
								AlignPosition.Responsiveness = 40
								AlignPosition.Position = Vector3.new(newPosition.X, newHeight - Humanoid.HipHeight, newPosition.Z)	-- Minus HipHeight on the Y axis to align the character's hands to the top of the wall
								AlignPosition.Enabled = true

								task.wait(0.5)					-- Small pause between the jump and climb for aesthetic reasons
								Ledge_JumpMid_Track:Stop()

								-- Ledge_ClimbUp & Ledge_ClimbForwards --
								Ledge_ClimbUp_Track:Play(0.1, 1, 0.6)

								AlignPosition.Responsiveness = 5							
								AlignPosition.Position = Vector3.new(newPosition.X, newHeight + Humanoid.HipHeight, newPosition.Z)	-- Plus HipHeight on the Y axis so that the character's legs will be above the wall

								task.wait(Ledge_ClimbUp_Track.Length)
								Ledge_ClimbForwards_Track:Play(0.1, 1, 0.6)

								AlignPosition.Responsiveness = 10
								AlignPosition.Position = Vector3.new(newPosition.X, newHeight + Humanoid.HipHeight + 0.5, newPosition.Z) + HumanoidRootPart.CFrame.LookVector * 3	
								-- Plus extra 0.5 studs on the Y axis to be safe so that the character can be above the wall and move character forwards by 3 studs

								task.wait(Ledge_ClimbForwards_Track.Length)

								-- Reset AlignPosition & Camera --
								AlignPosition.Enabled = false
								AlignOrientation.Enabled = false
								HitPart.CanCollide = true
								workspace.Camera.CameraSubject = Humanoid

							elseif HitPart.Size.Y > 2 then	-- Vault

								Controls:Disable()
								
								local AlignPosition = HumanoidRootPart:WaitForChild("AlignPosition")
								

								
								Halfwall_RunStart_Track:Play(0.1, 1, 1)

								local newPosition = HitPosition - (character.PrimaryPart.CFrame.LookVector * 1.3)
								local newHeight = HitPart.Size.Y/2 + HitPart.Position.Y

								AlignPosition.Responsiveness = 30
								AlignPosition.Position = Vector3.new(newPosition.X, HumanoidRootPart.Position.Y, newPosition.Z)
								AlignPosition.Enabled = true

								task.wait(Halfwall_RunStart_Track.Length/1.8)
								Halfwall_VaultUp_Track:Play(0.1,1,1)

								AlignPosition.Responsiveness = 30
								AlignPosition.Position = Vector3.new(newPosition.X, newHeight, newPosition.Z)


								task.wait(Halfwall_VaultUp_Track.Length/1.8)
								Halfwall_VaultForwards_Track:Play(0.1,1,1)

								AlignPosition.Responsiveness = 30
								AlignPosition.Position = HumanoidRootPart.Position + HumanoidRootPart.CFrame.LookVector * 3

								task.wait(Halfwall_VaultForwards_Track.Length/1.8)
								Halfwall_VaultLand_Track:Play(0.1,1,1)

								AlignPosition.Responsiveness = 30
								AlignPosition.Position = HumanoidRootPart.Position + HumanoidRootPart.CFrame.LookVector * 3

								task.wait(Halfwall_VaultLand_Track.Length/1.8)
								AlignPosition.Enabled = false

								HitPart.CanCollide = true
								workspace.Camera.CameraSubject = Humanoid
								
								
								
								
							end
									CameraModule.CameraMode("FirstPerson")
						end			
					end
				end
			end
			player.PlayerGui.StaminaBarGui.SprintScript.CanCrouch.Value = true
			Controls:Enable()
			isClimbing = false
		end
	end
end



ContextActionService:BindAction("ClimbCheck", ClimbCheck, false, Enum.KeyCode.Space)

1 Like

While I’m not able to provide an exact answer, I’d personally check the ClimbCheck function first before suspecting something else.

1 Like

Often times when a script stops working after a character dies and respawns, it’s because the script is using the old character instead of the new one. I see this a lot in LocalScripts within the Player.

Where you are declaring variables, you are declaring the character variable and defining it as player.Character or player.CharacterAdded:Wait(). This is completely fine. However, you are not re-defining it after the character respawns.

If you are using a variable to store a Player’s character, and the script is in the Player, you have to update the variable in case the character dies, in many situations.
Scripts inside the character ( copied from StarterCharacterScripts) will automatically “renew” when the character respawns, since the scripts are inside the character – if an Instance gets destroyed, its children get deleted also.

With that being said, you may have to update the other variables that hold the character’s children, too, like Humanoid.

3 Likes

I see you have two lines that have CameraModule.CameraMode(), and both happen to be in the ClimbCheck function. So, definitely give that a look. Definitely listen to the post above me as well, since you should definitely keep in mind it could just be the old character getting in the way.

1 Like