Need help on implementing lerp CFrame for my script

I made this part where it has rotation limits on Y orientation. It can be faced at the front and back and made it update in a loop using RunService. This lerp use a tween value then I made a variable alpha and left it there in a loop due to not knowing how to implementing it to my code. Here’s the code along with the footage of how this works below.

local Runservice = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Part = script.Parent
local repPart = workspace.Part
local Target = workspace.Target

Runservice.Stepped:Connect(function()
    local alpha = TweenService:GetValue(0.18, Enum.EasingStyle.Sine , Enum.EasingDirection.InOut)    

    local unitz = ((Part.Position - Target.Position).Unit).Z -- Determine where where the part is facing
    
    -- Facing to the front
    if  unitz == math.clamp(unitz,-1,-0.1) then
        Part.CFrame = CFrame.lookAt(Part.Position,Target.Position)
        Part.Orientation = Vector3.new(0,math.clamp(Part.Orientation.Y%360,135,225),0)

        
    end
    -- Facing to the back    
    if  unitz == math.clamp(unitz,0.1,1) then
        Part.CFrame = CFrame.lookAt(Part.Position,Target.Position) 
        Part.Orientation = Vector3.new(0,math.clamp(Part.Orientation.Y,-(45%360),45%360),0)    

    end
end)

To make it lerp, instead of directly setting the CFrame to the value you want, you store the “goal” value in a variable.

E.g. your goal might be:

goal = CFrame.lookAt(Part.Position,Target.Position)

Then, to set the CFrame, you do:

Part.CFrame = Part.CFrame:Lerp(goal, alpha)

The lerp function moves a CFrame towards a goal by alpha percent.

Note in this case, your alpha is constant, so it doesn’t really matter if you use the tween service to generate the alpha. You’d want to use the tween service for the alpha if it’s based on something, such as the distance or angle difference, e.g.:

local alpha = TweenService:GetValue(angleDifference/360, Enum.EasingStyle.Sine , Enum.EasingDirection.InOut)

I see. Also can you tell me where and how do you get angleDifference from?

You could do:

local _, ryCurrent, _ = currentCFrame:ToOrientation()
local dyCurrent = math.deg(ry)

local _, ryGoal, _ = goalCFrame:ToOrientation()
local dyGoal = math.deg(ry)

local angleDifference = (dyGoal - dyCurrent) % 360

But I would just use a fixed value, since even inputting that wouldn’t work exactly how the tween is defined. I don’t think it’s possible to follow a tween’s easing in a continuous way like you have for your turret.