Hello! I’ve been trying to learn about Lua more in depth so I could optimize the scripts I make in better ways and keep their foot prints as small as possible. Turns out one of the most intensive tasks for Lua is indexing things like tables or instances etc. so I wanna write a function that returns a position and an orientation from a given position and orientation kind of similar to CFrame.LookVector except without a table that has 12 diff numbers that I don’t need/not interested in. So far this is what I got:

The problem is it only works in x rotation/y position and I couldn’t get it to work on other axises to get full pointing capability I’d like to know if there are any alternatives to CFrame:LookAt(PosAt,Pos) etc. or if there is a possible way to get my script to work. I’ve been trying for 2 days but I’ve gotten nowhere, this is what it looks like in its current form:
local pi = math.pi
local sin, cos, tan = math.sin, math.cos, math.tan
local new, Vector3new, CFramenew, CFrameAngles, C3new = Instance.new, Vector3.new, CFrame.new, CFrame.Angles, Color3.new
local HBeat, TweenService = game:GetService("RunService").Heartbeat, game:GetService("TweenService")
local Size = Vector3new(.1,.1,.1)
local Pos = Vector3new(0,15,0)
for i = 1, 20 do
local Rot = (pi/10)*i
for i = 1, 20 do
local Part = new("Part",script)
Part.Size = Size
Part.Anchored = true
Part.Position = Vector3new(sin(Rot)*10*sin(i*(pi/10)),cos(i*(pi/10))*10,cos(Rot)*10*sin(i*(pi/10)))+Pos
end
end
function LookVector(X,Y,Mag)
return Vector3new(0,cos(Y)*Mag,cos(X)*Mag*sin(Y))
end
--[[function LookVector(X,Y,Mag)
return Vector3new(sin(X)*Mag,0,cos(X)*Mag)
end
function LookVector(X,Y,Mag)
return Vector3new(sin(X)*Mag*sin(Y),cos(Y)*Mag,cos(X)*Mag*sin(Y))
end]]
local Size2 = Vector3new(.3,.3,.3)
local Size3 = Vector3new(.3,.3,10)
for I = 1, 20 do
local Rot = (I/10)*I
for i = 1, 20 do
local Y = (i/10)*pi
local Part = new("Part",script)
Part.Size = Size2
Part.Anchored = true
Part.Color = C3new(1,0,0)
Part.Position = LookVector(Rot,Y,5) + Pos
local Part_ = new("Part",script)
Part_.Size = Size3
Part_.Anchored = true--CFrameAngles((pi/10)*15+Y,0,0) + LookVector(0,Y,5) + Pos
Part_.CFrame = CFrameAngles((pi/2)*3+Y,0,0) + LookVector(0,Y,5) + Pos --CFrameAngles(cos(Y+(pi*2)),Rot,0) + LookVector(Rot,Y,5) + Pos
wait()
end
end
Keep in mind the whole reason why I want something like this in the first place is to reduce lag via eliminating the unnecessary table/dictionary creation luau does every time you use something like CFrame:LookAt(PosAt,Pos).