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