Camera script breaks after death

Hello! when i’m dying, there is some bug appears. Video below.

If I reset, for example, this doesn’t happen.

I noticed that this is the script that creates this problem:

local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local players = game:GetService("Players")

CanViewBody = false
Sensitivity = 0.25
Smoothness = 0.055

spawn(function()
	while wait() do
		if workspace.CurrentCamera.FieldOfView == 25 then
			Sensitivity = 0.5
		elseif workspace.CurrentCamera.FieldOfView == 70 then
			Sensitivity = 0.25
		end
	end
end)

local player = players.LocalPlayer
local m = player:GetMouse()
local character = player.Character or player.CharacterAdded:wait()
local humanoidpart = character.HumanoidRootPart

local head = character:WaitForChild("Head")
local CamPos,TargetCamPos = workspace.CurrentCamera.CoordinateFrame.p,workspace.CurrentCamera.CoordinateFrame.p 
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0

local running = false
local freemouse = false

input.InputChanged:connect(function(inputObject)
	
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		local delta = Vector2.new(inputObject.Delta.x/Sensitivity, inputObject.Delta.y/Sensitivity) * Smoothness
		local X = TargetAngleX - delta.y 
		TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X 
		TargetAngleY = (TargetAngleY - delta.x) %360 

		CamPos = CamPos + (TargetCamPos - CamPos) * 0.28 
		AngleX = AngleX + (TargetAngleX - AngleX) * 0.35 
		local dist = TargetAngleY - AngleY 
		dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
		AngleY = (AngleY + dist *0.35) %360
	end	
	
end)

workspace.CurrentCamera.Changed:connect(function()
	if  (workspace.CurrentCamera.CFrame.p - head.Position).magnitude < 1 and not running then
		running = true
		runService:BindToRenderStep("RunFirstPerson", Enum.RenderPriority.First.Value + 1, function()
			if running then
				
				workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
				workspace.CurrentCamera.CoordinateFrame = CFrame.new(head.Position) 
				* CFrame.Angles(0,math.rad(AngleY),0) 
				* CFrame.Angles(math.rad(AngleX),0,0)
				* CFrame.new(0,0,0)
				
				humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0,math.rad(AngleY),0)
				else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
			end
			game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
		
		end)
	elseif (workspace.CurrentCamera.CFrame.p - head.Position).magnitude > 1 and not running then
		running = false
		runService:UnbindFromRenderStep("RunFirstPerson")
		
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
		workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	for i, v in pairs(character:GetChildren()) do
		if v:IsA("Accessory") then
			if v:FindFirstChildOfClass("Part") then
				v:FindFirstChildOfClass("Part").LocalTransparencyModifier = 1
			end
		end
	end
end)

If you understand the problem please tell me.

1 Like

try this

local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local players = game:GetService("Players")

CanViewBody = false
Sensitivity = 0.25
Smoothness = 0.055

spawn(function()
	while wait() do
		if workspace.CurrentCamera.FieldOfView == 25 then
			Sensitivity = 0.5
		elseif workspace.CurrentCamera.FieldOfView == 70 then
			Sensitivity = 0.25
		end
	end
end)

local player = players.LocalPlayer
local m = player:GetMouse()

local function setupCharacter(character)
	local humanoidpart = character:WaitForChild("HumanoidRootPart")
	local head = character:WaitForChild("Head")
	
	local CamPos, TargetCamPos = workspace.CurrentCamera.CoordinateFrame.p, workspace.CurrentCamera.CoordinateFrame.p 
	local AngleX, TargetAngleX = 0, 0
	local AngleY, TargetAngleY = 0, 0
	
	local running = false
	
	input.InputChanged:connect(function(inputObject)
		if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
			local delta = Vector2.new(inputObject.Delta.x / Sensitivity, inputObject.Delta.y / Sensitivity) * Smoothness
			local X = TargetAngleX - delta.y 
			TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X 
			TargetAngleY = (TargetAngleY - delta.x) % 360 

			CamPos = CamPos + (TargetCamPos - CamPos) * 0.28 
			AngleX = AngleX + (TargetAngleX - AngleX) * 0.35 
			local dist = TargetAngleY - AngleY 
			dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
			AngleY = (AngleY + dist * 0.35) % 360
		end	
	end)
	
	workspace.CurrentCamera.Changed:connect(function()
		if (workspace.CurrentCamera.CFrame.p - head.Position).magnitude < 1 and not running then
			running = true
			runService:BindToRenderStep("RunFirstPerson", Enum.RenderPriority.First.Value + 1, function()
				if running then
					workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
					workspace.CurrentCamera.CoordinateFrame = CFrame.new(head.Position)
						* CFrame.Angles(0, math.rad(AngleY), 0)
						* CFrame.Angles(math.rad(AngleX), 0, 0)
						* CFrame.new(0, 0, 0)
					
					humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0, math.rad(AngleY), 0)
				else
					game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
				end
				game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
			end)
		elseif (workspace.CurrentCamera.CFrame.p - head.Position).magnitude > 1 and not running then
			running = false
			runService:UnbindFromRenderStep("RunFirstPerson")
			game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
			workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		end
	end)
	
	game:GetService("RunService").RenderStepped:Connect(function()
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("Accessory") then
				if v:FindFirstChildOfClass("Part") then
					v:FindFirstChildOfClass("Part").LocalTransparencyModifier = 1
				end
			end
		end
	end)
end

-- Initial character setup
if player.Character then
	setupCharacter(player.Character)
end

-- Handle character respawn
player.CharacterAdded:Connect(function(character)
	setupCharacter(character)
end)
1 Like

I think you didn’t disconnect the camera connection so it prob is still going after death.

1 Like

no that didn’t helped for some reason :frowning:

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