Character dies every time I jump

I have this top down camera script and anytime the camera goes too high or my player jumps I immediately die

local camera = game.Workspace.CurrentCamera

local character = script.Parent
local hrp = character:WaitForChild('HumanoidRootPart')

camera.CameraType = Enum.CameraType.Scriptable

local cameraPart = Instance.new('Part')
cameraPart.Transparency = 1
cameraPart.CanCollide = false
cameraPart.Parent = game.Workspace

local mouse = game.Players.LocalPlayer:GetMouse()

game:GetService('RunService').RenderStepped:Connect(function()
	cameraPart.CFrame = CFrame.new(hrp.Position + Vector3.new(0, 25, 0), hrp.Position)

	camera.CFrame = cameraPart.CFrame
	
	hrp.CFrame = CFrame.new(hrp.Position, Vector3.new(mouse.Hit.Position.X, hrp.Position.Y, mouse.Hit.Position.Z))
	
	wait()
end)

How to fix?

You don’t need a wait() as what you are using is an event, not a loop.

I believe it has something to do with this line. Does removing it fixes the issue?

The issue is that, at some point, you are perfectly facing the primary part (I believe?), and getting NAN. I improved the script and tried to debug like this, in StarterPlayerScripts (to avoid setting a new RenderStepped connection every time the character spawns).

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local LocalPlayer = Players.LocalPlayer

local camera = Workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Scriptable

RunService.RenderStepped:Connect(function()
	local root = LocalPlayer.Character and LocalPlayer.Character.PrimaryPart
	if root == nil then
		return
	end
	
	local cframe = CFrame.lookAt(root.Position + Vector3.new(0, 25, 0), root.Position)
	print(cframe)
	camera.CFrame = cframe
end)

As I suspect, the print eventually gives NAN.

Since you are only ever top down, you don’t need to use the second parameter of CFrame.new (rewritten here as CFrame.lookAt) at all.

local cframe = CFrame.new(root.Position + Vector3.new(0, 25, 0)) * CFrame.Angles(-math.pi / 2, 0, 0)

I can then just throw this on at the end:

root.CFrame = CFrame.lookAt(root.Position, Vector3.new(mouse.Hit.Position.X, root.Position.Y, mouse.Hit.Position.Z))

Finished code:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local LocalPlayer = Players.LocalPlayer

local camera = Workspace.CurrentCamera
local mouse = LocalPlayer:GetMouse()

camera.CameraType = Enum.CameraType.Scriptable

RunService.RenderStepped:Connect(function()
	local root = LocalPlayer.Character and LocalPlayer.Character.PrimaryPart
	if root == nil then
		return
	end
	
	camera.CFrame = CFrame.new(root.Position + Vector3.new(0, 25, 0)) * CFrame.Angles(-math.pi / 2, 0, 0)
	root.CFrame = CFrame.lookAt(root.Position, Vector3.new(mouse.Hit.Position.X, root.Position.Y, mouse.Hit.Position.Z))
end)

2 Likes

hey, unfortunately your script doesn’t work either, in the console its showing NaN, NaN, NaN, etc. when I jump


edit: nevermind, i didn’t see the “finished code” part. thank you!

1 Like

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