How to update a tween?

Hello i want to know how can i update my tweens while they are playing

Lets take for example 2 parts
Part A and Part B

i want to make a tween that makes Part A fly to Part B

The thing is im constantly moving Part B around the map
how would i go on making a tween for it
I know there are better ways to do it but it was just an example and i want to use tweens for it

1 Like

When you play a new tween (assuming you’re modifying the same property), it will automatically override the previous tween that was already playing.

ok so how do i make it that Part A can only move 1000 studs ?

I’m not entirely sure what you’re asking here but I think you’re asking how to move a part 1000 studs using tweening, it would go something like this:

local tweenService = game:GetService("TweenService")
local partA = workspace.PartA

local tweenInfo = TweenInfo.new(
    10,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.In,
    0,
    false
)

local startPosition = partA.CFrame
local targetPosition = startPosition + Vector3.new(1000,0,0)

--move the part 1000 studs on the x axis
local tween = tweenService:Create(partA, tweenInfo, {CFrame = targetPosition})
tween:Play()

no what im asking is with in the same rules i said before but i add a condition that part A can only move 1000 studs

You could have a loop checking how far the part has moved, though this might not be the most precise depending on the speed of the tween:

local part = script.Parent

local lastposition = part.Position
local studsmoved = 0

while task.wait(0.5) do
	local distance = (part.Position - lastposition).Magnitude
	
	studsmoved += distance
	lastposition = part.Position

	print(studsmoved)
end

If you want to constrain the part to 1000 studs, you should multiply the unit vector of the subtraction of the position:

local tweenService = game:GetService("TweenService")
local partA = workspace.PartA
local partB = workspace.PartB

local tweenInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0,
	false
)

local startPosition = partA.CFrame
local targetPosition = CFrame.new(-(startPosition.Position - partB.Position).Unit * math.clamp((startPosition.Position - partB.Position).Magnitude, 0, 1000))

--move the part 1000 studs on the x axis
local tween = tweenService:Create(partA, tweenInfo, {CFrame = targetPosition})
tween:Play()
tween.Completed:Wait()
print(partA.Position)

will it work if i move part b around the map while the tween is playing?

This is the only limitation of tweens. You can, though you would need to loop the tween.

1 Like

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