CameraCFrame Killing Player [Help : Bug]

Good Day! , I am currently having a weird annoying bug. Its a bug that should not happen and only god knows why it happens. I coded a camera system (client module script) for my game which is a harmless and basic script. It only have two main function, SetCameraToParts and SetCameraToPlayer.
SetCameraToParts work perfectly fine, the problem is SetCameraToPlayer

The Script :

-------------------
--SERVICES

local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Camera = workspace.CurrentCamera
-------------------
--INSTANCE/VARIABLE

local CameraParts = workspace:WaitForChild("CameraPart")
local LocalPlayer = game:GetService("Players").LocalPlayer
local LocalMouse = LocalPlayer:GetMouse()

local SystemModule = ReplicatedStorage:WaitForChild("SystemModule")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local Functions = {["IsActive"] = Remotes:WaitForChild("Functions"):WaitForChild("IsActive")}
local ModuleHandler = require(SystemModule:WaitForChild("ModuleHandler"))

local CameraSystem = {}
local Modules = {}

------------
--MAIN CODE

-->> INITIALIZATION

function CameraSystem:Init()
	--MODULES
	
	Modules["PlayerModule"] = ModuleHandler:GetSystem("PlayerSystem")
	
	--MAIN INIT()
	repeat
		task.wait()
	until Camera.CameraType ~= Enum.CameraType.Fixed
	Camera.CameraType = Enum.CameraType.Scriptable
	
	self:SetCameraToPart("MainMenu")
	return true
end

-- >> FUNCTIONS

function CameraSystem:SetCameraToPart(PartName : string)
	RunService:UnbindFromRenderStep("PlayerCamera")
	if Camera.CameraType ~= Enum.CameraType.Scriptable then
		print("Camera Failed to Set")
		return
	end
	
	if CameraParts:FindFirstChild(PartName) or CameraParts:WaitForChild(PartName) then
		local Part = CameraParts[PartName]
		
		Camera.CFrame = Part.CFrame
	end
end
-------------------------------
local TargetCam : BasePart
local function PlayerCamera()
	if not TargetCam then
		return
	end
	
	--MAIN FUNCTIONALITY
	Camera.CFrame = CFrame.new(TargetCam.Position + Vector3.new(0,50,0),TargetCam.Position) --This breaks
	TargetCam.CFrame = CFrame.lookAt(TargetCam.Position, Vector3.new(LocalMouse.Hit.Position.X,TargetCam.Position.Y,LocalMouse.Hit.Position.Z))
end


function CameraSystem:SetCameraToPlayer()
	local Character = Modules["PlayerModule"]:GetCharacter()
	
	TargetCam = Character:FindFirstChild("HumanoidRootPart")
	RunService:BindToRenderStep("PlayerCamera",Enum.RenderPriority.Camera.Value,PlayerCamera)
end

return CameraSystem

(Im very new to modular coding so bare with me)

As you can see i have this specific line that seems harmless

Camera.CFrame = CFrame.new(TargetCam.Position + Vector3.new(0,50,0),TargetCam.Position)

For some weird reason, whenever i have this line of code enabled, when a player goes to a specific Y level or above, it instantly kills and make the player dissapear (?) when i make this line a comment, everything seems to be working fine. What could possible be wrong with this line of code? Also i do not have a anti-cheat system for this to happen nor have i put any free model in my game.

Video Context :

The Kill In the beginning is not the bug
SetCameraToPlayer function is only called when the player chose a class and click Play and when the player click respawn (thats why you can see player still alive before respawn button is clicked)

This only happens in high Y level , if the player is on the ground it works perfectly fine, but if player get too high it dies ??? (thats why the spawnpoint and the game spawn is on the sky)

Thank you for your time reading! , i’d greatly appreciate if you can help me debug this!

Also i just noticed

local Character = Modules["PlayerModule"]:GetCharacter()
	
	TargetCam = Character:FindFirstChild("HumanoidRootPart")
	RunService:BindToRenderStep("PlayerCamera",Enum.RenderPriority.Camera.Value,PlayerCamera)

Which is kinda a dumb thing for me to do when i just could do

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

So i changed it to that but the bug still occurs :pensive:
Also when i noticed when i die from that bug, a warning appear to my screen saying ā€œGameplay Pausedā€ meaning the player couldve been glitching very far or something?

You are definitely getting sent to infinity by some CFrame math going wrong. The only way I can think of how to catch it is to have a function that checks the CFrame for any NaNs, and triggers a breakpoint if it does. Then, try and follow the math back to see what’s causing it.

My idea is that the targetpart accelerated high to fast and some how got sent to the void , try lowering the y

Also make sure the target part is anchored

Hello, thanks for reply! Sorry im having problem understanding, how exactly do i make that function that check CFRames for NaNs? and how it would help? sorry im having trouble understanding huhu sorry!! but thanks for reply

Does the TargetPart need to be anchored? the TargetPart is a LocalPlayer HumanoidRootPart so it cant be anchored :frowning: . I’ll try the other one you suggested which is lowering the Y tho!

Edit : I Lowered the Y but now the player dies when reaching lower Y level than before? This is a weird bug bruh

I tried to debug more and for some weird reasons, this works

Old :

Camera.CFrame = CFrame.new(TargetCam.Position + Vector3.new(0,50,0),TargetCam.Position)

New :

Camera.CFrame = CFrame.new(TargetCam.Position + Vector3.new(0,50,0))

When i remove the CFrame looking at the TargetCam (which is the humanoidrootpart) and for some reason it doesnt kill anymore???

But i dont want this to happen cause i want the camera to face the player downwards

I do not know what causing this bug but solution will be appreciated! Thanks!!!

You don’t need constructing function with : instead .
Its euialent to:

local function module.MyFunction(self)

end

And you you never use self…

I dont understand why yall begginers is doing that, could you explain to me why becouse im kinda confused?
Also it would be wiser to just do

return {
function1 = function()

end;
function2 = function()

end
}

Without spending time and tanking resource consumption for table expansion and instead having it pre alocated and static…
You could also use tuple type:

return function()end,function()end

ModuleScript is just a function that runs once and every next time just returns cached result…That simple yet no tutorial teaches that and i find it frustrating :upside_down_face:
I personally avoid using ModuleScripts unless to share code between scripts becouse it mainly just ā€œbalkanizesā€ the code instead of promised ā€œorganizationā€

Also remember it runs only once after being required so ā€œInitā€ function or method is completely not needed to initiate it

1 Like

Umm actually this would result in a syntax error because you cannot have local members in a table.

1 Like

Oh right im writing it on mobile but you get the point.
I personally never even declear functions like this because im pro functional programming or minimalistic OOP

1 Like

Sir TargetCam is a humanoid root part, right?
Well you are editing this cframe in a function ā€œmethodāœØļøā€ PlayerCamera
That the entire cause of it :fire:

1 Like

Well that was kinda passive-aggresive. But i learnt something from what you said so thanks!

I did state that im kinda new to modular programming and OOP so my code wont look good and efficient since im just trying to grasp and try to apply it to practice (?). But i do see the point your trying to make! Thanks for the info! I’ll try to apply what you said next time :smile:

Ohh, sorry im having hard time understanding. Can you elaborate more on what you mean by "Editing CFrame in function ā€˜Method’ " ?

I already fixed the code by doing this

Camera.CFrame = CFrame.new(TargetCam.Position + Vector3.new(0,50,0)) * CFrame.Angles(-math.rad(90),0,0)

Which fixes the killing bug, but i still want to understand what you meant so i can help improve the code. Thanks!! :heart:

Nah im not angry at you im just kinda "funny":money_mouth_face:
I was just pointing out similar prolems i had when i were also new to that

1 Like
local function PlayerCamera()
	if not TargetCam then
		return
	end
	
	--MAIN FUNCTIONALITY
	Camera.CFrame = CFrame.new(TargetCam.Position + Vector3.new(0,50,0),TargetCam.Position) --This breaks
	TargetCam.CFrame = CFrame.lookAt(TargetCam.Position, Vector3.new(LocalMouse.Hit.Position.X,TargetCam.Position.Y,LocalMouse.Hit.Position.Z))
end

It does edit humanoid root part of character, causing it to fly out into the void likely and dying

1 Like

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