Setting Camera CFrame crashes the game

I’m trying to make a lock on system using a formula from another person

but when I try to print the CFrame, roblox studio sometimes crashes. If i try to set the camera cframe to it, it 100% crashes.
I saw another post where the game wasn’t working because the cframe number was too long, so I tried to round it but it still just crashes.

How can I fix?

Code:

local step = game:GetService("RunService").Heartbeat

local Player = game:GetService("Players").LocalPlayer

local Cam = workspace.CurrentCamera

local Offset = Vector3.new()
local Extra = 0

local function Formula(B: Vector3, A: Vector3)
	return B + ((A + Offset) - B).Unit * ((A - B).Magnitude+Extra), B
end
local function RoundCoordinate(cframe: CFrame | Vector3)
	local x, y, z = math.round(cframe.X), math.round(cframe.Y), math.round(cframe.Z)
	cframe = typeof(cframe) == 'CFrame' and CFrame.new(x,y,z) or Vector3.new(x,y,z)
	return cframe
end

local focused = nil
local connection
return function(Target: Model?)
	local Char = Player.Character
	if Target ~= nil and Char then
		print('co')
		if not Target:FindFirstChild("Humanoid") then return end
		Cam.CameraType = Enum.CameraType.Scriptable
		if not focused or focused ~= Target then
			print('c2')
			focused = Target
			local Pos, Lookat = Formula(Target.HumanoidRootPart.Position, Char.HumanoidRootPart.Position)
			Pos = RoundCoordinate(Pos)
			Lookat = RoundCoordinate(Lookat)
			print(Pos, Lookat)
			Cam.CFrame = CFrame.lookAt(Pos, Lookat)
			--Char.HumanoidRootPart.CFrame = CFrame.lookAt(Char.HumanoidRootPart.Position, Lookat)
		else
			focused = nil
			Cam.CameraType = Enum.CameraType.Custom
			if connection then
				connection:Disconnect()
				connection = nil
			end
		end
	end
end
1 Like

ngl this looks very odd, for future reasons i suggest looking at a style guide

I want to help you, however i feel like this is over-complicated for a lock-on camera

I fixed it by not referencing the localplayer in the script and not using lookat for the camera, but since it is on the client neither of these should cause the script to crash :frowning:

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