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