Script not rotating part around PivotOffset correctly

I’m trying to get the hand of the clock to rotate around its PivotOffset.

When using Studio’s rotation tool, it works as intended.
robloxapp-20230526-2051249.wmv (216.3 KB)

However, when attempting to rotate it from a script, it instead rotates around the center of mass.
robloxapp-20230526-2055065.wmv (630.4 KB)

I’d rather not use a HingeConstraint if possible.

Script:

-- Script is in ServerScriptService, hand is the clock hand, and duration is 10.
-- Yes, there's more to the script, but this is the only part that matters here.
for i = 1, duration * 10 do
	hand.Rotation += Vector3.new(0, 0, (-360 / duration) * 0.1)
	task.wait(0.1)
end
1 Like

Use :PivotTo instead of offsetting the rotation property of the part.

2 Likes

If you want to rotate the hand of the clock around its PivotOffset, you can achieve this by changing the rotation point of the hand. Instead of directly modifying the Rotation property of the hand, you can use the CFrame property to rotate the hand around a different pivot point.

-- Assuming the hand is a child of a Part or Model
local pivotOffset = hand.PivotOffset

-- Set initial rotation
hand.CFrame = hand.CFrame * CFrame.new(pivotOffset)

-- Script is in ServerScriptService, hand is the clock hand, and duration is 10.
-- Yes, there's more to the script, but this is the only part that matters here.
for i = 1, duration * 10 do
	local rotation = CFrame.Angles(0, 0, math.rad((-360 / duration) * 0.1 * i))
	hand.CFrame = hand.CFrame - hand.CFrame.Position * rotation + hand.CFrame.Position
	task.wait(0.1)
end
2 Likes

Thank you for your help! I used :PivotTo and it is now working as intented.

@IchVerwendeKeineTool
I appreciate your reply, but your code had a couple errors in it (lines 5 and 11, “invalid argument #1 (CFrame expected, got Vector3)”). However, I used the rotation value from line 10, so thank you for contributing that!

Final code:

for i = 1, duration / 10 do
    local currentPivot = hand:GetPivot()
    local rotation = CFrame.Angles(0, math.rad((360 / duration) * 0.1), 0)
    
    hand:PivotTo(currentPivot * rotation)
    
    task.wait(0.1)
end
2 Likes

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