Is there any way i can tween PivotTo?

local Trigger = script.Parent
local tree = workspace.FallingTree
local bgs = game:GetService("BadgeService")
local ts = game:GetService("TweenService")
local tinfo = TweenInfo.new(2)

local db = true
Trigger.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			local tween1 = ts:Create(tree,tinfo,{tree:PivotTo(tree:GetPivot() * CFrame.Angles(0, 0, math.rad(90)))}):Play() -- help here
			local tween2 = ts:Create(tree,tinfo,{Position = Vector3.new(517.935, 8.052, -28.476)}):Play()
			script.Parent.Sound:Play()
			wait(99999)
			db = true
		end
	end	
end)

if theres no way, can you tell me the replacement?

2 Likes

Tween the primarypart of the model, rather than the model itself.

Edit: Heres some pseudocode.

TweenService:Create(tree.PrimaryPart, TweenInfo.new(...), {CFrame = CFrame.new(...) or Position = Vector3.new(...)}:Play()

For your tween this wud work:

TweenService:Create(tree.PrimaryPart, tinfo, {CFrame = tree.PrimaryPart.CFrame * CFrame.Angles(0,90,0)}:Play()

This works bcuz the primarypart of a model is the pivot of all of its parts, so when it moves the other descendants of the model move accordingly, with a fixed offset.

FYI: You need to have welds attached to the models descendants

3 Likes

If you want a custom function, you can use this (this does not need any primary parts or welding to work):

local TWS = game:GetService("TweenService")

local function TweenModelToCFrame(model: Model, info: TweenInfo, result: CFrame)
	local cframe = Instance.new("CFrameValue") do
		cframe.Value = model:GetPivot()
	end
	
	cframe:GetPropertyChangedSignal("Value"):Connect(function()
		model:PivotTo(cframe.Value)
	end)
	
	local tween = TWS:Create(cframe, info, {Value = result})
	tween:Play()
	tween.Completed:Connect(function()
		cframe:Destroy()
	end)
end

TweenModelToCFrame(workspace.Model, TweenInfo.new(10), workspace.Model:GetPivot() + Vector3.new(50, 0, 0))
14 Likes

i changed the script, but the position wont change, it just rotates now

Oh mb i forgot to add the second tween.

ts:Create(tree.PrimaryPart, tinfo,{Position = Vector3.new(517.935, 8.052, -28.476)}):Play()

Also if u wanna reduce it to one tween do this:

local tween = ts:Create(tree.PrimaryPart ,tinfo, {CFrame = CFrame.new(517.935, 8.052, -28.476) * CFrame.Angles(0, math.rad(90), 0)})

tween:Play()
1 Like

Just to note, the behavior of SetPrimaryPartCFrame() and manually setting the primary part’s CFrame are different. If you don’t have welds connected to the primary part, manually setting the CFrame will not move the other parts with it (this is not the case with SetPrimaryPartCFrame()).

And if you are going to use SetPrimaryPartCFrame(), it’s better to just use PivotTo().

I assume the OP has welds attached to the models parts, but yes it is a valid point, I forgot to point it out.

You can lerp a ‘CFrame’ value and pass that as an argument to PivotTo.

local function TweenModel(Model : Model, Duration : number, GoalCFrame : CFrame) : (Model, number, CFrame) -> ()
	local Pivot : CFrame = Model:GetPivot()
	local Time : number = 0
	repeat
		Time += task.wait()
		local InterpolatedCFrame : CFrame = Pivot:Lerp(GoalCFrame, 1 - ((Duration - Time) / Duration))
		Model:PivotTo(InterpolatedCFrame)
	until Time > Duration
end

Here’s a demonstration using Roblox’s ‘Fountain’ model.
https://gyazo.com/0de727cf2848b253e037fe118b385523

6 Likes

Hey, I’m a bit late but i just fixed the problem with a function.

Here it is:

local tweens = {}

local runService = game:GetService("RunService")

function tweenPivot (object: Model, t: number, newCFrame: CFrame)
	if tweens[object] then tweens[object]:Disconnect() tweens[object] = nil end

    local startCFrame = object:GetPivot()
	local steps = 1 / t
	local currentStep = 0

	tweens[object] = runService.Stepped:Connect(function(t, dt)
		currentStep += steps * dt
		if currentStep > 1 then tweens[object]:Disconnect() tweens[object] = nil end

		object:PivotTo(startCFrame:Lerp(newCFrame, currentStep))
	end)

end

What is basicly does it. It checks for the time you want it to tween. Devides 1 through the time value.
This results in the needed alpha to add to currentStep. Do this times deltaTime (dt) to make sure its frame independed. then you lerp the cframes from current to new times currentStep. and quit once the currentStep is higher then 1.

To prevent multiple tweens from running add the same time its stored in a table under the instance, so when the same instance is requested for tween it will cancel the other one running.