Skip to content

Instantly share code, notes, and snippets.

Last active Jun 6, 2022
What would you like to do?
Rotation Pivots for Guis on Roblox
-- Pivot.lua by jaipack17
-- Last updated: 02/04/2022
-- License: MIT
-- Easily create rotation pivots for GuiObjects on Roblox.
-- The API is similar to the Pivot API available for BaseParts.
-- Pivot.SetAngleMode(angleMode: string) -> Set angle mode for the pivot. Either DEGREES or RADIANS. By default set to DEGREES.
-- Pivot.ApplyIgnoreGuiInsetOffset(apply: boolean) -> Apply Offset of, 36) to the center position of the GuiObject.
-- Set this to true if your ScreenGui is ignoring GuiInset.
-- GuiObject, pivotOffset: Vector2, keepPositionUnchanged: boolean, keepRotationUnchanged: boolean) -> Creates a pivot for the GuiObject given a pivot offset relative to the GuiObject's center
-- Pivot:Rotate(angle: number) -> Rotate the GuiObject around its pivot given an angle.
-- Pivot:GetPivotOffset() Vector2 -> Returns the pivot offset for the GuiObject
-- Pivot:GetPivot() Vector2 -> Returns the position of the pivot point on screen
-- Pivot:SetPivotOffset(pivotOffset: Vector2) -> Set a new pivot offset for the GuiObject.
-- Pivot:PivotTo(pivotPosition: Vector2) -> Move the pivot to somewhere else. This moves the GuiObject along with the pivot.
-- Pivot:CenterTo(centerPoisition: Vector2) -> Move the GuiObject to somewhere else. This moves the pivot along with the GuiObject.
local Pivot = {}
Pivot.__index = Pivot
local Settings = {
_angleMode = "DEGREES",
_applyIgnoreGuiInsetOffset = false
local function Offset(self)
return (if Settings._applyIgnoreGuiInsetOffset then, 36) else
local function GetCenter(self)
return self.Object.AbsolutePosition + self.Object.AbsoluteSize/2 + Offset(self)
function GuiObject, pivotOffset: Vector2, keepPositionUnchanged: boolean?, keepRotationUnchanged: boolean?)
local self = setmetatable({
Object = object,
PivotOffset = pivotOffset,
_applyIgnoreGuiInsetOffset = false,
_angleMode = "DEGREES",
_objectCenter = nil,
_pivotPosition = nil,
_overwritePosition = not keepPositionUnchanged,
_overwriteRotation = not keepRotationUnchanged
}, Pivot)
self._objectCenter = GetCenter(self)
self._pivotPosition = self._objectCenter + self.PivotOffset
return self
function Pivot.SetAngleMode(mode: string)
mode = mode:upper()
if mode == "DEGREES" or mode == "RADIANS" then
Settings._angleMode = mode
function Pivot.ApplyIgnoreGuiInsetOffset(apply: boolean)
Settings._applyIgnoreGuiInsetOffset = apply
function Pivot:Rotate(angle: number)
if Settings._angleMode == "DEGREES" then
angle = math.rad(angle)
local difference = self._objectCenter - self._pivotPosition
local theta = angle + math.atan2(difference.Y, difference.X)
local pos = self._pivotPosition +, math.sin(theta)) * difference.Magnitude
local offset =, .5) - self.Object.AnchorPoint
offset *= self.Object.AbsoluteSize
pos -= offset
if self._overwritePosition then
self.Object.Position = UDim2.fromOffset(pos.X, pos.Y)
if self._overwriteRotation then
self.Object.Rotation = math.deg(angle)
function Pivot:GetPivot()
return self._pivotPosition
function Pivot:GetPivotOffset()
return self.PivotOffset
function Pivot:SetPivotOffset(pivotOffset: Vector2)
self.PivotOffset = pivotOffset
self._objectCenter = GetCenter(self)
self._pivotPosition = self._objectCenter + self.PivotOffset
function Pivot:CenterTo(centerPosition: Vector2)
self._objectCenter = centerPosition
self._pivotPosition = self._objectCenter + self.PivotOffset
function Pivot:PivotTo(pivotPosition: Vector2)
self._pivotPosition = pivotPosition
self._objectCenter = self._pivotPosition - self.PivotOffset
return Pivot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment