Tweening CFrame causes it to destroy itself

Hello, I have a model im trying to tween to hover back and forth. For some reason despite me declaring origin and goal CFrames, when it plays the tween it will move slowly to the target then suddenly start teleporting to random positions and eventually out of the world.

Here is my code:

local root = script.Parent.Main

local origin = CFrame.new(root.Position.X,root.Position.Y,root.Position.Z) --* CFrame.Angles(0,math.rad(0),0)
local goal = CFrame.new(root.Position.X,root.Position.Y+5,root.Position.Z) --* CFrame.Angles(0,math.rad(180),0)

local tween = game:GetService("TweenService"):Create(root,TweenInfo.new(10,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{CFrame = goal})
local back = game:GetService("TweenService"):Create(root,TweenInfo.new(10,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{CFrame = origin})

root.corrupt:Play()

while wait() do
	tween:Play()
	tween.Completed:Wait()
	back:Play()
	back.Completed:Wait()
end

Any help is appreciated!

Is root an anchored part? Unanchored it will behave erratically. If it’s a model/has more pieces to it consider using :SetPrimaryPartCFrame() instead.

You could also factor out the back tween by having tween's TweenInfo’s “Reverses” parameter set to true, and maybe even “repeat” set to -1 for it to infinitely play.

It wasnt anchored, thank you so much

1 Like