SetPrimaryPartCFrame teleports player to negative infinity?

I am teleporting the player’s model in a bare world with only the flat baseplate, and 1% of the time, the model is being teleported to negative infinity below the map, causing the player’s death. You can see the target position and the actual resulting position in the log output below.

This seems like a really basic issue. What am I missing about how player Models need to be teleported?

Here’s the only relevant code in the project:

--code that's being called approximately once per second 
print("MOVINGTO:", newPosition)
player_model:SetPrimaryPartCFrame(CFrame.new(newPosition,lookSpot))
--This is the only code in project that changes player model/parts

--code that's run in a separate script
print("ACTUALPOSITION: ",player_RootPart.Position)
--output log result
  08:26:02.541  MOVINGTO: -3.2999999523163, 0.40000000596046, 4.5  -  Server - RelocateAllWrongModels:16
  08:26:02.573   ▶ACTUALPOSITION: 0, -3.4028234663853e+38, 0 (x15)  -  Server - PlayerStartupSS:74
  08:26:03.090  MOVINGTO: -3.2999999523163, 0.40000000596046, 4.5  -  Server - RelocateAllWrongModels:16
  08:26:03.090  Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.  -  Server - RelocateAllWrongModels:17

After some testing I believe this happens sometimes when the first and second parameter of CFrame.new are the same. I think a solution to this would be setting the PrimaryPartCFrame only when the newPosition and lookSpot are different.

1 Like

I changed my code to ensure that lookSpot (the lookAt Vector) would never equal newPosition (pos Vector), and it appears that you are correct! So long as the CFrame’s pos and lookAt vectors aren’t identical, the model isn’t teleported to negative infinity in Y anymore.

Many thanks! I wonder what kind of keywords I can post in this thread so that this solution is easily searchable…

1 Like

Please use new Pivots instead of deprecated PrimaryPartCFrame.

I second his suggestion. Basically you would just have to replace SetPrimaryPartCFrame with PivotTo. Function arguments and everything else would remain the same; you’d basically just replace the function name with a shorter one. The result will be improved, especially if the player is moved many times. I’d still keep the fix you made, of course.

1 Like

Interesting. I didn’t notice because SetPrimaryPartCFrame wasn’t marked as deprecated in the API.

Thanks to you both for pointing that out.

1 Like