I’m trying to tween a models rotation and position, and im doing it one by one in a cycle like this:
while wait() do
local RotTween = TweenService:Create(Primary, TweenInfo.new(0.5), {CFrame = CFrame.lookAt(Primary.Position, Target)})
RotTween:Play()
RotTween.Completed:Wait()
local MoveTween = TweenService:Create(Primary, TweenInfo.new((Primary.Position - Target).Magnitude / Speed, Enum.EasingStyle.Linear), {CFrame = CFrame.new(Target) * Primary.CFrame.Rotation})
MoveTween:Play()
MoveTween.Completed:Wait()
task.wait(2)
end
Now, this works, but only for a few cycles until the model suddenly teleports to a ridiculous Y level like so: -3.4028234663852886e+38
I have no idea why this happens as it happens completely randomly after only a few cycles, as well as the fact I’m doing both tweens independently at different times.
If you use this lookAt constructor, you need to check that the arguments are not the same. If the distance from Primary.Position to Target is zero, this will give you an error CFrame with NAN values. So once your move tween has made the part reach the target position, the rotation tween’s goal CFrame becomes bogus and assigning that bogus CFrame to your Primary part yeets it out of existence.
local TweenService = game:GetService("TweenService")
while true do
local targetCFrame = CFrame.lookAt(Target.Position, Primary.Position)
local tweenTime = (Primary.Position - Target.Position).Magnitude / Speed
local tween = TweenService:Create(Primary, TweenInfo.new(tweenTime, Enum.EasingStyle.Linear), {CFrame = targetCFrame})
tween:Play()
tween.Completed:Wait()
task.wait(2)
end
Yeah thanks this was a super simple fix, I just added a BeforeTarget variable that updates each frame and checked if it was different to the actual target. Had no idea about that weird thing about CFrames, so thanks for that.