CFrame movement adds up instead of starting fresh each time

I have a seat that the player gets out of when a button is pushed.
I want the player to stand in front of the seat, not on top of it, so I used this bit of code:

humanoidRootPart.CFrame *= CFrame.new(0, 0, -2)

When the player leaves the seat they are -2 studs in front of the seat.

The problem is that the -2 studs keeps adding to itself and the player is placed farther and farther in front of the seat each time they stand up.

I checked the HumanoidRootPart and it is staying with the character as it should.
I tried reseting the HumanoidRootPart.CFrame to equal itself each time before the script is run to refresh it but that didn’t work either.

Anyone know why this is happening?

I also tried this line of code:

character:PivotTo(character:GetPivot() * CFrame.new(0, 0, -2))

The same thing happens. It keeps adding up instead of refreshing.

humanoidRootPart.CFrame = Seat.CFrame + Seat.CFrame.LookVector * Vector3.new(2, 0, 2)

Thank you so much!

Problem solved.

But, can you tell me why it did not work the other way?
I’m just confused and would like to understand.

To prevent the player from being placed farther and farther in front of the seat each time they stand up, you can try using the CFrame.new() function to set the player’s position relative to the seat instead of using the *= operator. This way, the player’s position will be set to a specific location every time the button is pushed, rather than being offset from their previous position.

For example, you could use the following code to set the player’s position to be two studs in front of the seat:

humanoidRootPart.CFrame = seat.CFrame * CFrame.new(0, 0, -2)
Alternatively, you could use the SetPrimaryPartCFrame() method on the player’s Character object to set the player’s position. This method works similarly to the CFrame property, but it also updates the player’s orientation so that they are facing the same direction as the seat.

Here is an example of how you could use this method to set the player’s position and orientation:

local character = player.Character
if character then
  local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
  if humanoidRootPart then
    character:SetPrimaryPartCFrame(seat.CFrame * CFrame.new(0, 0, -2))
  end
end

I hope this helps! Let me know if you have any other questions.

1 Like

Thank you both!

I appreciate your detailed info. I’m still lost as to why it did not work to just use the hrp’s current location as a starting point.

But that’s okay. I got a lot to do and don’t need to understand every little thing just yet! :grinning:

Simply put:
= stands for is now in Lua.
x *= y is short for x = x * y
With cframes, x *= y means that the cframe X is being translated by Y. So Y is added relative to X.

So you would use = instead of *= since you do not want many translations, you just wanted to set the cframe.

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