How can I make CFrame turn smoother

So this script works, but I noticed that after some time the object will start to twitch or bug out a bit from local or server side, is there a way to make this even smoother or anything


while wait() do
	if Left == true then
		local Turret = game.Workspace.Battery
		local TurretPrimary = Turret.PrimaryPart
		Degree = Degree + 0.5
		Turret:SetPrimaryPartCFrame(CFrame.new(TurretPrimary.Position) * CFrame.fromEulerAnglesXYZ(0, math.rad(Degree), math.rad(90)))
		RunService.Stepped:Wait()
	end
	if Right == true then
		local Turret = game.Workspace.Battery
		local TurretPrimary = Turret.PrimaryPart
		Degree = Degree - 0.5
		Turret:SetPrimaryPartCFrame(CFrame.new(TurretPrimary.Position) * CFrame.fromEulerAnglesXYZ(0, math.rad(Degree), math.rad(90)))
		RunService.Stepped:Wait()
	end
end
2 Likes

Instead of CFrame, you can use the TweenService to get smoother turning.

With tween service you can’t rotate how much you want

You can, just use the Rotation property, or change the CFrame

The issue is you are using both wait() and RunService wait(). That’s definitely going to cause more delays than just your wait().

Also it’s not smooth because of the degree increments which is always 0.5 and hence there will be a noticeable gap with each degree added.

If you want your turret to turn with constant speed consider CFrame lerping like in my resource:

Also I don’t advise set primary part CFrame at all it’s already choppy and has one glaring as is and will especially get worse when used a lot more like in a turret, you can alternately use this module or the recommended way of using a Motor6D Rig.

You can use the tweenservice.

local TS = game:GetService("TweenService")
local Tween = TS:Create(Turret.PrimaryPart, TweenInfo.new(0.1), {CFrame=CFrame.new()})

Obviously mess around with it to get it how you want. It’s quite simple actually.

Heres the docs for TweenService

I never actually tried tweening or anything like that how would I specify the goal for the CFrame though

try lerping your cframe or smthg

Here is the way the TweenService works.

First you have to identify the target object you want to manipulate. I am assuming it would be a part within the model ‘Turret’.

The next part is how long you want the tween to last. The last part is the table of properties you want to change, or basically the goal of the CFrame.

So an example:

local part = Instance.new("Part", workspace)
part.Position = Vector3.new(0,0,0)
local TS = game:GetService("TweenService")
local Tween = TS:Create(part, TweenInfo.new(0.1), {CFrame=CFrame.new(0,0,5)})
Tween:Play()

This will move the part 5 studs backwards (relative to orientation and position) smoothly. If you read the docs it will give you a better explanation. :+1:

You could also try lerping like @Pure_Bacn said in his reply.