How to tween a model's orientation

I have been looking at this for hours, and still can’t find an answer to this problem.
I want the orientation of this model to rotate randomly, but in a smooth manner.
I keep getting errors like: SpinElevator.FloorScript:8: invalid argument #1 to ‘new’ (Vector3 expected, got CFrame) - Studio

etc.

Code:

local Floor = {}

function Floor:Start(RFloor)
	local NormalPos = CFrame.new(-249.926, 90.178, -2937.036)
	
	for i = 1, RFloor.Time.Value do
		local Goal = {}
		Goal:SetPrimaryPartCFrame(CFrame.new(NormalPos * CFrame.fromEulerAnglesXYZ(0, 90, math.random(-180, 180))))
		
		local Time = TweenInfo.new(0.5)
		
		local TweenElevator = game:GetService("TweenService"):Create(game.Workspace.RealElevator, Time, Goal)
		TweenElevator:Play()
		
		wait(0.6)
	end
	
	game.Workspace.RealElevator:SetPrimaryPartCFrame(NormalPos * CFrame.fromEulerAnglesXYZ(0, 90, 0))
	script.Parent.Done.Value = true
end

return Floor

Any suggestions will help!

2 Likes

Take a look at this:

1 Like

I think you should set “Goal” as a CFrame because “Goal:SetPrimaryPartCFrame()” will not work (since it isn’t a model). And as for the tweening, you should tween only the model’s primary part (and weld other parts to the primary part, remember to anchor the primary part and unanchor those “other parts”) because tweening a whole model won’t work.

1 Like

Before I take a closer look at this and test it, they aren’t using any orientation methods like I had in my script, would it still worked if I added it?

1 Like

I’ve been able to move the orientation with the primary part WITHOUT welding, but I just wanted to tween it. I also think “Goal” means game.Workspace.RealElevator, just represented as a variable. (Could be wrong)

1 Like

You should use a CFrameValue if you want to tween a model. To tween its orientation you will just need to change CFrameValue’s angles, here’s an example (you should implement it with your own code).

local TS = game:GetService("TweenService")

local Chair = workspace.Chair
local TweenValue: CFrameValue = Chair.TweenValue
TweenValue.Value = Chair:GetPivot()

local Info = TweenInfo.new() -- all defaults

local Tween = TS:Create(TweenValue, Info, {Value = Chair:GetPivot()*CFrame.Angles(0, math.pi/2, 0)})

TweenValue.Changed:Connect(function()
	Chair:PivotTo(TweenValue.Value)
end)

task.wait(5)

Tween:Play()

Ill try that tomorrow, thanks!
Ill update you all on how it goes.

So its giving me this error Capture

Code:

local Floor = {}

function Floor:Start(RFloor)
	local NormalPos = CFrame.new(-249.926, 90.178, -2937.036)
	
	for i = 1, 17 do
		local TweenService = game:GetService("TweenService")
		local Elevator = game.Workspace.RealElevator
		local TweenValue: CFrameValue = Elevator.TweenValue
		TweenValue.Value = Elevator:GetPivot()

		local Info = TweenInfo.new(1) -- all defaults

		local Tween = TweenService:Create(TweenValue, Info, {Value = Elevator:GetPivot()*CFrame.Angles(0, 90, math.random(-180, 180))})

		TweenValue.Changed:Connect(function()
			Elevator:PivotTo(TweenValue.Value)
		end)

		Tween:Play()
	end
	
	game.Workspace.RealElevator:SetPrimaryPartCFrame(NormalPos * CFrame.fromEulerAnglesXYZ(0, 90, 0))
	script.Parent.Done.Value = true
end

return Floor

You’re creating an IntValue for tweening, instead use a CFrameValue.

Oh okay, let me try that. Thank you for all the help btw!

You can use the following code to “tween” a model’s orientation:

local RunService = game:GetService("RunService")
local stepped = RunService:IsClient() and RunService.RenderStepped or RunService.Heartbeat
	
local function tweenModel(model, destination, time)
	local startTime = tick()
	local origin = model.PrimaryPart.CFrame
	
	local connection connection = stepped:Connect(function()
		local currentTime = tick()
		local dt = currentTime - startTime
		
		if dt >= time then
			connection:Disconnect()
			
			model:SetPrimaryPartCFrame(destination)
		else
			model:SetPrimaryPartCFrame(origin:Lerp(destination, dt/time))
		end
	end)
end

The reason I say “tween” is because this does not allow for the extra customization that is provided with the TweenService, outside of letting you tween linearly (via linear interpolation) over a set amount of time.

You can also add on to this to make a method that purely “tweens” a mode’s orientation, not its position whatsoever:

local function tweenOrientation(model, targetOrientation, time)
	tweenModel(model, targetOrientation - targetOrientation.p + model.PrimaryPart.CFrame.p, time)
end

Now you can call either the tweenModel() method if you want to tween both position and orientation, and the tweenOrientation() method if you want to just tween orientation. In either case the first parameter is the model to tween, the second is the target CFrame, and the third is the amount of time the tween should take to complete.

1 Like

I got it to work, thanks! Will keep this in mind

Isn’t this worse than using tween service? When I mean worse I mean like laggy and glitching