I have tried to make a script where a part get's rotated around a pivot point, but as usual, I'm a skrub who can't get it working.
local testmodel = game.Workspace.TestModel function rotatemodel(model,center,rotation) for i,v in ipairs(model:GetChildren()) do v.CFrame=((CFrame.new(center)*rotation)*CFrame.new((v.Position-center)))*(v.CFrame-v.Position) end end rotatemodel(testmodel,testmodel.PivotPoint.Position,CFrame.Angles(0,math.pi/2,0))
This is why I'm called the most skrubiest person in teh world of roboxia and shcriptin halpers
We'll first need to find the offset of the part to the pivot point. Knowing the part's position and the position of the pivot point, we can do this by taking the inverse of the CFrame returned by the toObjectSpace method.
Now that the offset is known, we can calculate the CFrame by multiplying the pivot CFrame by the desired angle and lastly the offset.
Example:
local pivot = CFrame.new() local part = game.Workspace.Part local offset = part.CFrame:toObjectSpace(pivot):inverse() local angle = CFrame.Angles(0, math.rad(30), 0) part.CFrame = pivot*(part.CFrame - part.CFrame.p)*angle*offset
We will need to use some sort of looping structure in order to interpolate the rotation so that it appears as an animation. I will be using a linear interpolation as it is easiest to implement.
I'll show you two of the ways I like to do this, one dependant on duration and one dependant on speed.
Dependant on Duration:
local pivot = CFrame.new() local part = game.Workspace.Part local offset = part.CFrame:toObjectSpace(pivot):inverse() local initialRotation = part.CFrame - part.CFrame.p local firstTick = tick() local elapsed = 0 local duration = 1 local desiredAngle = 30 while elapsed <= duration do local alpha = math.min(1, elapsed/duration) local angle = desiredAngle*alpha part.CFrame = pivot*initalRotation*angle*offset elapsed = tick() - firstTick end
Dependant on Speed:
local pivot = CFrame.new() local part = game.Workspace.Part local offset = part.CFrame:toObjectSpace(pivot):inverse() local initialRotation = part.CFrame - part.CFrame.p local firstTick = tick() local elapsed = 0 local speed = 16 -- deg/sec local desiredAngle = 30 local duration = desiredAngle/speed while elapsed <= duration do local alpha = math.min(1, elapsed/duration) local angle = desiredAngle*alpha part.CFrame = pivot*initalRotation*angle*offset elapsed = tick() - firstTick end