How can I teleport a model after it finished moving

I have a car model in my game that comes from a tunnel and enters another in a loop.
At first, I wanted to have it move the entire the way, even when you can’t see it, but then I found out every time it does a loop it goes 0.001 studs to the right, a minor change but noticeable in the long run.
So now I’m trying to have it teleport when it enters the tunnel but I can’t figure out how.
I have a main part welded to the other parts in the model that moves the car.

while true do
 wait()
 	for i = 1,32 do
 	script.Parent.CFrame = script.Parent.CFrame + Vector3.new(0,0,-4)	--car goes forward
 	wait()
end

for i = 1,10 do
script.Parent.CFrame = script.Parent.CFrame + Vector3.new(0,0,-3)
 wait()
script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(9),0) * CFrame.new(0,0,4)	--car turns left
 wait()
end

for i = 1,300 do
 script.Parent.CFrame = script.Parent.CFrame + Vector3.new(-4,0,0)	--car goes forwardLONG
 wait()
end

for i = 1,10 do
 script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(-9),0) * CFrame.new(0,-1,0) --car goes down and turns right
 wait()
end


end

You can set a PrimaryPart to the model and use either :SetPrimaryPartCFrame( CFRAME ) or :MoveTo( POISITION ) to teleport the model after. (I recommend using SetPrimaryPartCFrame so that the model does not collide with others)

Since you said you have one main part welded the others, you can simply make the main part the PrimaryPart.

2 Likes

Right now it’s just standing there after it finishes moving.
I did:

Main:SetPrimaryPartCFrame(-646.549, -12.643, 2402.607) 

Try using this (only change I did is wrap the CFrame values in a CFrame.new)

Main:SetPrimaryPartCFrame(CFrame.new(-646.549, -12.643, 2402.607)) 
1 Like

Mh, Same as before.
Does it matter where I place it?
I placed it right after the “car goes down” part and I placed the local variable right at the start.

You can print the CFrame of the main part right after you teleport it, to see if it’s moving accordingly. Maybe the loop you made with the while true do is overwriting the CFrame movement. If you haven’t already, you should enable Output to rule out any technical errors.

1 Like

You could just mark the position of the car before the loop starts and move the car to that position every time the loop reaches the end.

For example:

local OrigCF = script.Parent.CFrame
while true do
     -- Loop Code here
     script.Parent.CFrame = OrigCF
end
1 Like

I got “SetPrimaryPartCFrame is not a valid member of Part”

You need to use the SetPrimaryPartCFrame on the model rather than the part.

1 Like

Oh that explains it, but then it will teleport before the moving script finishes right?

If it’s the last thing in the loop, then yes.

1 Like

Nevermind, thought you meant I had to make a script inside the model. I finally got it now, thanks a lot!

1 Like