Tweening a model is sometimes sending it into the void

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.

Thanks for your help.

is your tween part anchored? Check if its anchored.

Are you getting errors at this line? Usually, I create tweens by using ["CFrame"], in brackets.

Also,

What is Target? A basepart? If it’s a basepart I believe you may have intended to use Target.Position

Let just ask what you’re trying to do, because there may be a better way to do it. X Y problem, you know?

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.

1 Like
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.

1 Like

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