A better way to rotate a part?

I am making an obby and I am making a spinning part, It works alright it is just kinda laggy sometimes and I want it to be a lot smoother like on tower of hell. The is the script that is in the part.

while true do
	wait()
	Part.CFrame = Part.CFrame * CFrame.new(0, 0, 0) * CFrame.fromEulerAnglesXYZ(0, Factor, 0)
	Part.RotVelocity = Vector3.new(0,Factor*30,0)
end

Anyone know another way I can do this that will make it smoother.

You could use an attachment and a constraint

maybe you could use tweening

local Part = .... 

game.TweenService:Create(
	Part,
	TweenInfo.new(
		5, -- Time (The Less Number The Faster Goal)
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		-1 -- Heres the repeat count (negative num will repeat the tween infinitely)
	),
	{
		CFrame = Part.CFrame * CFrame.fromEulerAnglesXYZ(0, math.pi, 0),
	}
):Play()

there are actually contrains and body movers can rotate a basepart better also which is easier

1 Like

If you’re spinning it on the server, chances are there’s no way for you to make it super smooth (ping is going to delay changes, fps may vary, etc.) but you could try looping an N second tween to math.pi and then resetting the CFrame to have an angle of 0 (so that it can be repeated, otherwise the part is already at math.pi and it just won’t move.) Or, as cookie detailed, make the tween an indefinite loop.
Edit: Accidentally deleted a bit of the paragraph above, fixed.

Example: (Very similar to Cookie’s code, just in a function format)

local function RotatePart(Part, Factor)
  local Tween = game:GetService("TweenService"):Create(
    Part,
    TweenInfo.new(
      Seconds,
      Enum.EasingStyle.Sine,
      Enum.EasingDirection.InOut,
      -1
    ),
    {
      CFrame = Part.CFrame * CFrame.fromEulerAnglesXYZ(0, (Factor or math.pi), 0)
    }
  )
  Tween:Play()
  return Tween -- Allows you to have full control over if it stops,
  -- and if you wish to, also detect when it's finished.
end

Cheers.

3 Likes

Try this:

while wait() do
	script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 0.015, 0)
end
local run = game:GetService("RunService")
local tweens = game:GetService("TweenService")

local part = workspace:WaitForChild("Part")

run.RenderStepped:Connect(function(deltaTime)
	local tween = tweens:Create(part, TweenInfo.new(0.1), {CFrame = part.CFrame * CFrame.Angles(0, math.pi * deltaTime, 0)})
	tween:Play()
end)

For a smoother animation you could depend on the client instead of the server as it handles physics replication more optimally.