GuiHandler
Gives each ScreenGui a handler that shows and hides it with a tween instead of a hard
Enabled toggle. A handler owns a main container (the top-level frame of the gui) and an
animation config that says what the container's properties should be when shown, what they
should be when hidden, and which TweenInfo to use for each direction. By default the
container tweens between the centre of the screen (UDim2.fromScale(0.5, 0.5)) and a position
above the screen (UDim2.fromScale(0.5, -0.5)) over 0.25 seconds.
Show enables the gui and tweens the container to its shown properties; Hide tweens the
container to its hidden properties and then disables the gui. Both accept an instant flag
that sets the properties immediately instead of tweening, and both fire the Shown / Hidden
/ Toggled signals so other scripts can react. Every handler is registered in a module-level
list, so ShowAll, HideAll, GetGuiHandler and WaitForGuiHandler let any client script
work with guis it did not create.
GetOffScreenPosition computes a scale position just outside a given edge of the screen for a
GuiObject, which makes it easy to build slide-in/slide-out configs with
newAnimationConfig.
The module requires the client and errors when required on the server. It returns a table with
the functions listed on this page plus new (the constructor), DefaultAnimationConfig,
GuiHandlers and GuiHandlerAdded.
local Players = game:GetService("Players")
local GuiHandler = require(path.to.GuiHandler)
local playerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local shopGui = playerGui:WaitForChild("Shop") :: ScreenGui
local shopFrame = shopGui:WaitForChild("Main") :: Frame
-- Slide the shop in from the right and hide it again by sliding out to the right
local config = GuiHandler.newAnimationConfig({
ShowProperties = { Position = UDim2.fromScale(0.5, 0.5) },
HideProperties = { Position = GuiHandler.GetOffScreenPosition(shopFrame, Vector2.new(1, 0)) },
ShowTweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
HideTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
} :: any)
-- Start hidden; toggling ShopGui.Enabled from elsewhere will animate too
local shop = GuiHandler.new(shopGui, shopFrame, false, true, config)
shop.Toggled:Connect(function(isShowing)
print("Shop is now", isShowing and "open" or "closed")
end)
local openButton = playerGui:WaitForChild("Hud"):WaitForChild("OpenShop") :: TextButton
openButton.Activated:Connect(function()
if shop.IsShowing then
shop:Hide()
else
shop:Show()
end
end)
Credits: Trove and Signal are by sleitnick (sleitnick's RbxUtil). Wally installs both.
Installation and guide: GuiHandler package page.
Types
GuiAnimationConfig
interface GuiAnimationConfig {ShowProperties: {[string]: any}--
Property values applied to the main container when showing (tween target, or set directly when instant). Default { Position = UDim2.fromScale(0.5, 0.5) }.
HideProperties: {[string]: any}--
Property values applied to the main container when hiding. Default { Position = UDim2.fromScale(0.5, -0.5) }, just above the top of the screen.
ShowTweenInfo: TweenInfo--
TweenInfo used for the show tween. Default TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut).
HideTweenInfo: TweenInfo--
TweenInfo used for the hide tween. Default TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut).
}
Describes how a handler animates its main container. The property tables are passed straight
to TweenService:Create, so any tweenable property of the container works (Position, Size,
BackgroundTransparency, Rotation, ...). Build one with newAnimationConfig to fall back to
the defaults for fields you leave out, or read DefaultAnimationConfig for the stock values.
Properties
Gui
This item is read only and cannot be modified. Read OnlyGuiHandler.Gui: ScreenGui
The ScreenGui this handler manages. Show sets its Enabled to true before animating and
Hide sets it to false after animating.
IsShowing
This item is read only and cannot be modified. Read OnlyGuiHandler.IsShowing: boolean
Whether the handler considers the gui shown. Initialised from Gui.Enabled in new, set to
true at the start of Show and false at the start of Hide (before the tween plays).
Show/Hide are no-ops when this already matches.
AnimationConfig
GuiHandler.AnimationConfig: GuiAnimationConfig
The config used by Animate. Defaults to DefaultAnimationConfig; replace it with
SetAnimationConfig.
MainContainer
This item is read only and cannot be modified. Read OnlyGuiHandler.MainContainer: GuiObject?
The GuiObject that is tweened. new requires one, but if it is ever missing Animate warns
and skips the animation (the gui is still enabled/disabled).
Trove
This item is read only and cannot be modified. Read OnlyGuiHandler.Trove: Trove
The Trove holding the handler's tweens and the
Enabled listener. It is attached to Gui and MainContainer, so destroying either cleans it.
Destroy cleans it as well.
Shown
This item is read only and cannot be modified. Read OnlyGuiHandler.Shown: Signal<>Fires after Show has finished (after the show tween completes, or immediately when instant).
Hidden
This item is read only and cannot be modified. Read OnlyGuiHandler.Hidden: Signal<>Fires after Hide has finished and the gui has been disabled.
Toggled
This item is read only and cannot be modified. Read OnlyGuiHandler.Toggled: Signal<boolean>
Fires with true whenever Shown fires and false whenever Hidden fires, so you can watch
both transitions with one connection.
DefaultAnimationConfig
This item is read only and cannot be modified. Read OnlyModuleGuiHandler.DefaultAnimationConfig: GuiAnimationConfig
Available on the module table (GuiHandler.DefaultAnimationConfig). The config used when new
is given no AnimationConfig and the source of fallback values for newAnimationConfig: tween
Position between UDim2.fromScale(0.5, 0.5) and UDim2.fromScale(0.5, -0.5) over 0.25 s with
Quint/InOut easing. Do not mutate it; build your own with newAnimationConfig.
GuiHandlers
This item is read only and cannot be modified. Read OnlyModule
Available on the module table (GuiHandler.GuiHandlers). The live registry of every handler
created with new and not yet destroyed, in creation order. Iterate it to inspect all guis; do
not insert or remove entries yourself.
GuiHandlerAdded
This item is read only and cannot be modified. Read OnlyModule
Available on the module table (GuiHandler.GuiHandlerAdded). Fires with the new handler at the
end of every new call. WaitForGuiHandler is built on it.
Functions
ShowAll
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsGuiHandler.ShowAll(instant: true?--
Skip the tweens and snap every gui to its shown state.
) → ()
Calls Show(instant) on every registered handler, one after another in registration order.
Because an animated Show waits for its tween to finish, the guis appear sequentially and this
function yields until the last one is done; pass instant = true to show everything at once
without yielding.
HideAll
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsGuiHandler.HideAll(instant: true?--
Skip the tweens and snap every gui to its hidden state.
) → ()
Calls Hide(instant) on every registered handler, one after another in registration order.
Animated hides run sequentially and this function yields until the last tween completes; pass
instant = true to hide everything immediately without yielding. Handy for cutscenes or a
"hide HUD" setting.
WaitForGuiHandler
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Returns the handler registered for gui, yielding until one is created with new if it does
not exist yet. Use this from scripts that run before the script that constructs the handler.
There is no timeout: if no handler is ever created for that gui, the call never returns.
local hud = GuiHandler.WaitForGuiHandler(playerGui:WaitForChild("Hud"))
hud:Hide()
GetGuiHandler
Returns the handler registered for gui, or nil if none has been created (or it has been
destroyed). Does not yield; use WaitForGuiHandler if the handler may not exist yet.
GetOffScreenPosition
GuiHandler.GetOffScreenPosition() → UDim2--
Scale position just outside the chosen edge(s) of the screen.
Computes a UDim2 (scale only) that places guiObject fully outside the screen on the side
given by direction, taking the object's Size scale and AnchorPoint into account. The
direction is normalised and each axis rounded to -1, 0 or 1, so Vector2.new(-1, 0) means off
the left edge, Vector2.new(0, 1) off the bottom, and Vector2.new(1, 1) off the bottom-right
corner. An axis whose direction is 0 keeps the object's current position scale.
The result is meant to be used as the Position in a HideProperties (or ShowProperties)
table for slide-in/slide-out animations. Only the Scale components of Size and Position
are considered; pixel offsets are ignored.
local offLeft = GuiHandler.GetOffScreenPosition(frame, Vector2.new(-1, 0))
NOTE
offset is validated but not applied in the current version; the returned position is the same
with or without it.
Errors
| Type | Description |
|---|---|
| "Parameter passed is not a valid GuiObject" | `guiObject` is not a `GuiObject`. |
| "direction must be a Vector2" | `direction` is not a Vector2. |
| "offset must be a Vector2 or nil" | `offset` is given but is not a Vector2. |
newAnimationConfig
Builds a complete GuiAnimationConfig from a partial one: every field that is nil in
config is filled with the value from DefaultAnimationConfig. Returns a new table; the
argument is not modified. Pass the result to new or SetAnimationConfig.
The parameter is typed as a full GuiAnimationConfig, so under --!strict you may need to
cast a partial table ({ ... } :: any) when omitting fields.
local config = GuiHandler.newAnimationConfig({
HideProperties = { Position = UDim2.fromScale(0.5, 1.5) }, -- slide out the bottom
} :: any)
new
GuiHandler.new(ShowByDefault: boolean?,--
true to show instantly, false to hide instantly, nil to leave as is.
RunAutomatically: boolean?,--
Animate whenever Gui.Enabled changes externally.
) → GuiHandler--
The registered handler.
Creates a handler for Gui, registers it so GetGuiHandler / WaitForGuiHandler / ShowAll
/ HideAll can find it, and fires GuiHandlerAdded. Exposed on the module as
GuiHandler.new.
IsShowingstarts asGui.Enabled.-
ShowByDefault:truecallsShow(true)andfalsecallsHide(true)right away (both instant);nilleaves the gui as it is. Note thatShow/Hideare no-ops whenIsShowingalready matches, so the container is only snapped into place when the state actually changes. -
RunAutomatically: whentrue, changingGui.Enabledfrom anywhere (another script, the Studio explorer) triggers an animatedShow/Hideto match. This lets you drive the gui with the plainEnabledproperty and still get tweens. -
The handler's Trove is attached to both
GuiandMainContainer, so destroying either cleans up the tweens and listener. CallDestroyto also remove the handler from the registry.
Errors
| Type | Description |
|---|---|
| "Invalid gui parameter, must be ScreenGui" | `Gui` is not a `ScreenGui`. |
| "Invalid MainContainer parameter, must be GuiObject" | `MainContainer` is not a `GuiObject`. |
SetAnimationConfig
Replaces the handler's AnimationConfig. Takes effect on the next Show/Hide; a tween that is
already playing is not affected. Use newAnimationConfig to build a config that falls back to
the defaults.
Animate
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsGuiHandler:Animate(showing: boolean,--
true to apply ShowProperties, false to apply HideProperties.
instant: true?--
Set the properties immediately instead of tweening.
) → ()
Applies the shown or hidden properties to MainContainer. Show and Hide call this for you;
call it directly only when you want to replay the animation without changing IsShowing or
Gui.Enabled.
- Does nothing while
Gui.Enabledisfalse(a disabled gui cannot be animated). - Warns and does nothing if there is no
MainContaineror noAnimationConfig. -
With
instant, each property inShowProperties/HidePropertiesis assigned directly (a failed assignment is warned about, not raised). -
Otherwise a tween is created with the matching
TweenInfo, added to the Trove, played, and this method yields until the tween completes.
Show
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsGuiHandler:Show(instant: true?--
Snap to the shown properties instead of tweening.
) → ()
Shows the gui: sets IsShowing to true, enables Gui, runs Animate(true, instant) to move
the container to its shown properties, then fires Shown (and therefore Toggled(true)).
Does nothing if IsShowing is already true.
When not instant this yields for the duration of ShowTweenInfo before Shown fires.
Hide
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsGuiHandler:Hide(instant: true?--
Snap to the hidden properties instead of tweening.
) → ()
Hides the gui: sets IsShowing to false, runs Animate(false, instant) to move the container
to its hidden properties, disables Gui once that is done, then fires Hidden (and therefore
Toggled(false)). Does nothing if IsShowing is already false.
When not instant this yields for the duration of HideTweenInfo before the gui is disabled and
Hidden fires.
Destroy
GuiHandler:Destroy() → ()
Unregisters the handler (so GetGuiHandler, ShowAll and HideAll no longer see it) and
cleans its Trove, cancelling in-flight tweens and disconnecting the RunAutomatically
listener. The ScreenGui itself is not destroyed or hidden, and the handler's signals are not
destroyed.