Tween issue I'm having

So I’m just trying to tween a part slightly 5 studs away from it’s current position and then back. But for some reason it flys way away, what am I doing wrong?

task.wait(8)
function tweenPart(part)
	local TweenService = game:GetService("TweenService")

	part.Position = Vector3.new(part.Position)
	part.Anchored = true

	local tweenInfo = TweenInfo.new(
		8, -- Time
		Enum.EasingStyle.Linear, -- EasingStyle
		Enum.EasingDirection.Out, -- EasingDirection
		-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
		true, -- Reverses (tween will reverse once reaching it's goal)
		0 -- DelayTime
	)

	local tween = TweenService:Create(part, tweenInfo, {Position = part.Position - Vector3.new(5,0,0)})

	tween:Play()
	print("tweening")
end

tweenPart(script.Parent)

I think the reason its flying away is because you keep subtracting the position in the property table after the tween.

To fix this you should store its original position in a variable like this:

local oriPosition = part.Position

Then you can create the tween again:

local tween = ts:Create(part, tweenInfo, {Position = oriPosition - Vector3.new(5,0,0)})

This should solve the part flying away
Let me know if you have any questions!

1 Like