Camera script kills players

I’m making a super simple custom overhead camera script and for some reason it randomly kills the player! I’m serious, the script could not be simpler, and for some reason it’s killing the player in a loop! I had this same problem a long time ago and I have no idea what could have caused it.

Code (StarterCharacterScripts)
local camera = game.Workspace:WaitForChild("Camera")

camera.CameraType = Enum.CameraType.Scriptable

local char = script.Parent or game.Players.LocalPlayer.CharacterAdded:Wait()
local hrp:Part = char:WaitForChild("HumanoidRootPart")
game["Run Service"].RenderStepped:Connect(function()
	camera.CFrame = CFrame.new(Vector3.new(hrp.Position.X, script:GetAttribute("Distance"), hrp.Position.Z), hrp.Position)
end)

Edit: The same thing happens when I put it in StarterPlayerScripts and change the script.parent part to game.Players.LocalPlayer.Character

I managed to stay alive for a second but the moment my height changed I died again.

Are you sure it’s that script killing the player, cause I don’t see anything that would kill the player there. Changing the camera doesn’t affect the character in any way.

That’s this thing! All it does is change the camera and the character bugs out and dies as soon as its height changes. Is there another way that you know of to make an overhead camera?

I dont know what else is in your game. Did you try running that script by itself in a empty place?

I’m a little bit confused with “its height changes”, the character’s height? Why would the character’s height change by manipulating the camera unless it’s somehow binded to it.

I meant the character’s HumanoidRootPart Y position value

The game right now is pretty barebones (there’s basically no other scripts), I tried an extremely similar script a while ago and the same thing happened.

Why are you changing the HRP’s position? You’re not supposed to mess with the character at all, just change the camera’s CFrame to be above the character.

I’m not changing the HRP position, I’m moving the camera to a CFrame that’s based on it’s position

game["Run Service"].RenderStepped:Connect(function()
	camera.CFrame = CFrame.new(Vector3.new(hrp.Position.X, 25, hrp.Position.Z), hrp.Position)
end)

I’m gonna check this out in a new place real quick, though I’m 99% sure that the camera script isn’t killing the character.

1 Like

Ok, if you copy the script just change the script:GetAttribute("Distance") part to 25 or any arbitrary number, or add an attribute to the script. Make sure it’s in StarterCharacterScripts

1 Like

Managed to fix it and surprisingly it was the camera script, to be specific the formula you used to set the CFrame. Just replace it with this and it should work,

	camera.CFrame = CFrame.new(hrp.Position + (hrp.CFrame.UpVector * script:GetAttribute("Distance"))) * CFrame.Angles(math.rad(-90), 0, 0)

Thanks, it worked! Do you know why the original didn’t work?

You’re welcome. I’m not 100% sure as to why but the way you were getting the angle to be pointed at the character was the cause, this part , hrp.Position)

It’s due to a NAN error, looking directly down with CFrame.lookAt or CFrame.new() creates instabilities

local camera = game.Workspace:WaitForChild("Camera")

camera.CameraType = Enum.CameraType.Scriptable

local char = script.Parent or game.Players.LocalPlayer.CharacterAdded:Wait()
local hrp:Part = char:WaitForChild("HumanoidRootPart")
game["Run Service"].RenderStepped:Connect(function()
	print(char.HumanoidRootPart.Position)
	camera.CFrame = CFrame.new(Vector3.new(hrp.Position.X, script:GetAttribute("Distance"), hrp.Position.Z), hrp.Position)
end)

image

3 Likes

This is likely a bug. I am able to consistently kill myself by changing my camera cframe in the command bar by using some specific ranges of valid values.
Strange it does not kill when I am knocked over in the ragdoll state.

1 Like