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
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()
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)