How would I tween a parts CFrame orientation so that it's the exact number I give it?

I want to tween a part so that the degrees I give it is the degrees it goes too, not the degrees in which it turns… if that makes sense. So if I say rotate to 60 Y, instead of the part rotating 60 degrees, it rotates till it gets to 60 degrees. I have a basic tween system here:

local number = 1
local tweenService = game:GetService("TweenService")

local positions = {
	script.Parent.Parent.PrimaryPart.CFrame * CFrame.fromOrientation(0,math.rad(60),0)
}


local debounce = false
script.Parent.Triggered:Connect(function(player)
	if debounce == false then
		debounce = true
		local tween = tweenService:Create(script.Parent.Parent.PrimaryPart, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {CFrame = positions[number]})
		tween:Play()
		tween.Completed:Wait()
		number += 1
		if not positions[number] then
			number = 1
		end
		debounce = false
	end
end)

I only have 1 value in the table at the moment but when you press a prompt, it rotates. Currently it only rotates 60 degrees but I want it to rotate till it gets to 60 degrees.

If you don’t care about the orientation of script.Parent.Parent.PrimaryPart, then you only need to do

local positions = {
	CFrame.new(script.Parent.Parent.PrimaryPart.Position) * CFrame.fromOrientation(0,math.rad(60),0)
}

Also, you can just use CFrame.Angles.

Lastly, this is entirely optional of course, but I’d recommend writing a function like this to make tweening easier in the future:

local T = game:GetService('TweenService')
function tween(o,t,l,s,d)
	s = s or Enum.EasingStyle.Linear
	d = d or Enum.EasingDirection.InOut
	local i = TweenInfo.new(l,s,d)
	return T:Create(o,i,t)
end

That way you can just do tween(instance,{Property=Value},lengthOfTime,style,direction) (style and direction are both optional).

bruh no way it was that easy, thank you. Also noted about the tweening. Quick question though, why do you say s = s instead of just s = Enum.EasingStyle.Linear

If I did that, it would overwrite the argument for s if one was given. Saying thing = thing means it sets the variable to itself, then saying or after means that if the first thing isn’t there (aka nil), then do the second thing.

Basically, it’s a more efficient way of doing:

if not s then
	s = Enum.EasingStyle.Linear
end

The end result is an optional argument with a default value if one is not given.

1 Like

Oh cool, i’ll look into using that more. Thanks again!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.