:GetPivot().Rotate is always zero

I have a script:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bed = ReplicatedStorage:WaitForChild("Bed")

local UserInputService = game:GetService("UserInputService")
local Status = false

local Mouse = Player:GetMouse()
local object: Instance? = nil

UserInputService.InputBegan:Connect(function(Button)
	if Button.KeyCode == Enum.KeyCode.E and Status == false then
		Status = true
		
		object = Bed:Clone()
		
		object:PivotTo(Mouse.Hit)
		object.Parent = workspace
	elseif Button.UserInputType == Enum.UserInputType.MouseButton1 and Status == true then
		Status = false
		object = nil
	elseif Button.KeyCode == Enum.KeyCode.R and Status == true then
		object:PivotTo(object.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(object:GetPivot().Rotation.Y + 90), object:GetPivot().Rotation.Z))
	elseif Button.KeyCode == Enum.KeyCode.T and Status == true then
		object:PivotTo(object.PrimaryPart.CFrame * CFrame.Angles(0, object:GetPivot().Rotation.Y, math.rad(object:GetPivot().Rotation.Z + 90)))
	end
end)

Mouse.Move:Connect(function()
	if Status == true and object ~= nil then
		Mouse.TargetFilter = object
		local MousePosition = Mouse.Hit

		object:PivotTo(MousePosition)
		object:PivotTo(CFrame.new(object:GetPivot().Position.X, object:GetPivot().Position.Y + 2, object:GetPivot().Position.Z) * CFrame.Angles(object:GetPivot().Rotation.X, object:GetPivot().Rotation.Y, object:GetPivot().Rotation.Z))
	end
end)

and it always outputs a zero in object.:GetPivot().Rotation

I don’t understand why this is happening, but what misleads me the most is that it doesn’t happen in lines 25 and 27.
How do I make it so that it doesn’t give out zero, but gives out real values?

As you can see in this video, the object is successfully rotated, but as soon as I move it a little, it immediately returns to zero Rotations.

I would be very grateful for your help.

1 Like

Man, why don’t you just do

object:PivotTo(object:GetPivot() * CFrame.Angles(0,0,math.pi/2))

Hello,

Your issue was caused by trying to access rotation values incorrectly. The correct way is to use CFrame:ToEulerAnglesYXZ(), which provides valid Yaw, Pitch, and Roll angles. Let me know if this works

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bed = ReplicatedStorage:WaitForChild("Bed")

local UserInputService = game:GetService("UserInputService")
local Status = false

local Mouse = Player:GetMouse()
local object: Instance? = nil

UserInputService.InputBegan:Connect(function(Button)
	if Button.KeyCode == Enum.KeyCode.E and Status == false then
		Status = true
		object = Bed:Clone()
		object:PivotTo(Mouse.Hit)
		object.Parent = workspace
	elseif Button.UserInputType == Enum.UserInputType.MouseButton1 and Status == true then
		Status = false
		object = nil
	elseif Button.KeyCode == Enum.KeyCode.R and Status == true and object then
		local _, ry, rz = object:GetPivot():ToEulerAnglesYXZ()
		object:PivotTo(object.PrimaryPart.CFrame * CFrame.Angles(0, ry + math.rad(90), rz))
	elseif Button.KeyCode == Enum.KeyCode.T and Status == true and object then
		local rx, ry, _ = object:GetPivot():ToEulerAnglesYXZ()
		object:PivotTo(object.PrimaryPart.CFrame * CFrame.Angles(rx, ry, math.rad(90)))
	end
end)

Mouse.Move:Connect(function()
	if Status == true and object ~= nil then
		Mouse.TargetFilter = object
		local MousePosition = Mouse.Hit
		object:PivotTo(MousePosition)
		local rx, ry, rz = object:GetPivot():ToEulerAnglesYXZ()
		object:PivotTo(CFrame.new(object:GetPivot().Position.X, object:GetPivot().Position.Y + 2, object:GetPivot().Position.Z) * CFrame.Angles(rx, ry, rz))
	end
end)

Unfortunately, this didn’t help. Not only did this not solve the problem, but it also did something strange. The model spins with the mouse movement.
robloxapp-20250327-0508225.wmv (1.6 MB)
Sorry me for the quality of the video, it is so due to the fact that I used the built-in Roblox Studio recorder.

Oh try this instead then:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bed = ReplicatedStorage:WaitForChild("Bed")

local UserInputService = game:GetService("UserInputService")
local Status = false

local Mouse = Player:GetMouse()
local object: Instance? = nil

UserInputService.InputBegan:Connect(function(Button)
    if Button.KeyCode == Enum.KeyCode.E and Status == false then
        Status = true
        object = Bed:Clone()
        
        object:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p))  -- Place the object at mouse position
        object.Parent = workspace
    elseif Button.UserInputType == Enum.UserInputType.MouseButton1 and Status == true then
        Status = false
        object = nil
    elseif Button.KeyCode == Enum.KeyCode.R and Status == true then
        -- Get the current rotation using ToEulerAnglesYXZ
        local rx, ry, rz = object:GetPivot():ToEulerAnglesYXZ()
        -- Apply a 90 degree rotation on the Y-axis (Yaw)
        object:SetPrimaryPartCFrame(object.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(90), 0))
    elseif Button.KeyCode == Enum.KeyCode.T and Status == true then
        -- Get the current rotation using ToEulerAnglesYXZ
        local rx, ry, rz = object:GetPivot():ToEulerAnglesYXZ()
        -- Apply a 90 degree rotation on the Z-axis (Roll)
        object:SetPrimaryPartCFrame(object.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(90)))
    end
end)

Mouse.Move:Connect(function()
    if Status == true and object ~= nil then
        -- Prevent the object from moving too much by locking it to the mouse's Y position.
        local MousePosition = Mouse.Hit

        -- Update the object's position based on the mouse
        object:SetPrimaryPartCFrame(CFrame.new(MousePosition.p.X, MousePosition.p.Y + 2, MousePosition.p.Z))  -- Keep it slightly above the ground
    end
end)

The bug with rotating the model around the cursor disappeared, but the main problem remained.

Hey! This should work. You were setting the object’s position directly to where the mouse is, which would reset the rotation because you had two :PivotTo() functions.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bed = ReplicatedStorage:WaitForChild("Bed")

local UserInputService = game:GetService("UserInputService")
local Status = false

local Mouse = Player:GetMouse()
local object: Instance? = nil

UserInputService.InputBegan:Connect(function(Button)
	if Button.KeyCode == Enum.KeyCode.E and Status == false then
		Status = true

		object = Bed:Clone()

        --Made this only position so the rotation doesn't go wonky
		object:PivotTo(CFrame.new(Mouse.Hit.Position))
		object.Parent = workspace
	elseif Button.UserInputType == Enum.UserInputType.MouseButton1 and Status == true then
		Status = false
		object = nil
	elseif Button.KeyCode == Enum.KeyCode.R and Status == true then
		object:PivotTo(object.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(object:GetPivot().Rotation.Y + 90), object:GetPivot().Rotation.Z))
	elseif Button.KeyCode == Enum.KeyCode.T and Status == true then
		object:PivotTo(object.PrimaryPart.CFrame * CFrame.Angles(0, object:GetPivot().Rotation.Y, math.rad(object:GetPivot().Rotation.Z + 90)))
	end
end)

Mouse.Move:Connect(function()
	if Status == true and object ~= nil then
		Mouse.TargetFilter = object
		local MousePosition = Mouse.Hit.Position
		
        --Combined the two :PivotTo()'s into one.
		object:PivotTo(CFrame.new(MousePosition+Vector3.new(0, 2, 0))*object:GetPivot().Rotation)
        --You were using :GetPivot().Rotation wrong, since it returns a CFrame with no positional, and you were putting in X,Y,Z, it would always be 0,0,0
	end
end)

Let me know if this helps!

1 Like

Yes, it helped. Thank you for your help! And also thanks to everyone who tried to help!

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