GuiButton
GuiButton is an abstract class that inherits from GuiObject. It is the base class for ImageButton and TextButton. Objects of this type serve to be interactive, clickable user interface elements. It defines several properties for interaction behavior, namely GuiButton.AutoButtonColor and GuiButton.Modal, as well as a handful of events for mouse buttons (GuiButton.MouseButton1Click, GuiButton.MouseButton1Down, etc).
The most important event of a GuiButton is GuiButton.Activated, a multi-platform event that fires when the button is activated. When using a mouse, this means clicking the button and releasing with the cursor still over the UI object. For touch, the same applies but with a touch instead of button press. Finally, for gamepads, GuiButton.Activated fires if a GuiButton is selected when the A-button is pressed and released. In short, this event is very useful for multi-platform user interface programming as it provides a nice general interface for a single user input.
Summary
Properties
Determines whether the button automatically changes color when the mouse hovers over or clicks on it.
If true while the GUI element is visible, the mouse will not be locked unless the right mouse button is down.
A boolean property which indicates whether the object has been selected.
Sets the style of the GuiButton based on a list of pre-determined styles.
Determines whether this UI element sinks input.
Determines the origin point of a GuiObject, relative to its absolute size.
Determines whether resizing occurs based on child content.
Determines the GuiObject background color.
Determines the transparency of the GuiObject background and border.
Determines the color of the GuiObject border.
Determines in what manner the GuiObject border is laid out relative to its dimensions.
Determines the pixel width of the GuiObject border.
Determines if descendant GuiObjects outside of the bounds of a parent GUI element should render.
Controls the sort order of the GuiObject when used with a UIGridStyleLayout.
Sets the GuiObject which will be selected when the gamepad selector is moved downward.
Sets the GuiObject which will be selected when the gamepad selector is moved to the left.
Sets the GuiObject which will be selected when the gamepad selector is moved to the right.
Sets the GuiObject which will be selected when the gamepad selector is moved upward.
Determines the pixel and scalar position of the GuiObject.
Determines the number of degrees by which the GuiObject is rotated.
Determine whether the GUI can be selected by a gamepad.
Overrides the default selection adornment used for gamepads.
The order of GuiObjects selected by the gamepad UI selection.
Determines the pixel and scalar size of the GuiObject.
Sets the Size axes that the GuiObject will be based on, relative to the size of its parent.
A mixed property of BackgroundTransparency and TextTransparency.
Determines whether the GuiObject and its descendants will be rendered.
Determines the order in which a GuiObject renders relative to others.
Describes the actual screen position of a UI element, in pixels.
Describes the actual screen rotation of a UI element, in degrees.
Describes the actual screen size of a UI element, in pixels.
When set to true, localization will be applied to this GuiBase2d and its descendants based on the GuiBase2d.RootLocalizationTable specified for this GuiBase2d.
A reference to a LocalizationTable to be used to apply automated localization to this GuiBase2d and its descendants.
Customizes gamepad selection behavior in the down direction.
Customizes gamepad selection behavior in the left direction.
Customizes gamepad selection behavior in the right direction.
Customizes gamepad selection behavior in the up direction.
Allows customization of gamepad selection movement.
Methods
Methods inherited from GuiObject- TweenPosition(endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : bool,callback : function):bool
Smoothly moves a GUI to a new UDim2.
- TweenSize(endSize : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : bool,callback : function):bool
Smoothly resizes a GUI to a new UDim2.
- TweenSizeAndPosition(endSize : UDim2,endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : bool,callback : function):bool
Smoothly moves a GUI to a new size and position.
Events
Fires when the button is activated.
Fired when the mouse has fully left clicked the GUI button.
Fired when the mouse is in the left mouse down state on the GUI object.
Fired when the left mouse has released the GUI object.
Fired when the mouse has fully right clicked the GUI button.
Fired when the mouse is in the right mouse down state on the GUI object.
Fired when the right mouse button has been released on a GUI Object.
Fired when a user begins interacting via a Human-Computer Interface device (Mouse button down, touch begin, keyboard button down, etc).
Fired when a user changes how they're interacting via a Human-Computer Interface device (Mouse button down, touch begin, keyboard button down, etc).
Fired when a user stops interacting via a Human-Computer Interface device (Mouse button down, touch begin, keyboard button down, etc).
Fires when a user moves their mouse into a GUI element.
Fires when a user moves their mouse out of a GUI element.
Fires whenever a user moves their mouse while it is inside a GUI element.
Fires when a user scrolls their mouse wheel back when the mouse is over a GUI element.
Fires when a user scrolls their mouse wheel forward when the mouse is over a GUI element.
Fired when the GuiObject is being focused on with the Gamepad selector.
Fired when the Gamepad selector stops focusing on the GuiObject.
Fires when the player starts, continues and stops long-pressing the UI element.
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
Fires when the player moves their finger on the UI element.
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
Fires when the player performs a pinch or pull gesture using two fingers on the UI element.
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
Fires when the player performs a rotation gesture using two fingers on the UI element.
Fires when the player performs a swipe gesture on the UI element.
Fires when the player performs a tap gesture on the UI element.
- SelectionChanged(amISelected : bool,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
Fires when the gamepad selection moves to, leaves, or changes within the connected GuiBase2d or any descendant GuiObjects.
Properties
AutoButtonColor
The AutoButtonColor determines whether the button automatically changes color when the user's Mouse hovers over or clicks on it.
If true, the button will automatically change color when the mouse hovers over or clicks on it. If false, the button will not change.
If you would like to customize how a button changes when the user's mouse hovers over or clicks on it, consider using an ImageButton GUI and changing the element's ImageButton.HoverImage and ImageButton.PressedImage.
Please note that this property will not have an effect on an ImageButton if it's ImageButton.Image property is set to an image and is not null. Additionally, the property will not affect an ImageButton element on mouse hover when its ImageButton.HoverImage is not null nor on mouse click if ImageButton.PressedImage is not null.
Code Samples
local button = script.Parent
if button:IsA("ImageButton") then
if button.HoverImage and button.ClickImage then
return
end
end
button.AutoButtonColor = true
Modal
If true while the GUI element is visible, the mouse will not be locked unless the right mouse button is down.
Methods
Events
Activated
Fires when the button is activated. As this event doesn't fire on the server, it should only be used in a LocalScript, or in a Script with RunContext of Enum.RunContext.Client.
Parameters
MouseButton1Click
The MouseButton1Click event fires when the user's Mouse fully left clicks the GUI button.
By clicking, the mouse has to be in bounds of the button and has to be pressed down and up again before this event fires. If the mouse leaves the bounds of the button and is released, the event will not fire. If you would like to avoid this limitation, you can use GuiButton.MouseButton1Down and MouseButton1Up. These events are similar, but will fire whenever the user pressed their left mouse down or up, respectively.
This event is similar to GuiButton.MouseButton2Click, which behaves identically except that it is connected to the user's right mouse button.
Note that this event will only fire for GUI buttons, including TextButtons and ImageButton. It will not fire for other GuiObjects.
Code Samples
local button = script.Parent
local function leftClick()
print("Left mouse click")
end
local function rightClick()
print("Right mouse click")
end
button.MouseButton1Click:Connect(leftClick)
button.MouseButton2Click:Connect(rightClick)
MouseButton1Down
The MouseButton1Down event fires when the user presses their left Mouse button down on the GUI object.
This event is similar to GuiButton.MouseButton2Down, which behaves identically except that it is connected to the user's right mouse button.
If you are looking for an event requiring the user to press and release their left mouse on a GUI in order for the event to fire, consider using GuiButton.MouseButton1Click.
Note that this event will only fire for GUI buttons, including TextButtons and ImageButton. It will not fire for other GuiObjects.
Parameters
The mouse's x screen coordinate in pixels.
The mouse's y screen coordinate in pixels.
Code Samples
local button = script.Parent
local function leftMouseButtonUp(x, y)
print("Left mouse up at", x, y)
end
local function leftMouseButtonDown(x, y)
print("Left mouse down at", x, y)
end
button.MouseButton1Up:Connect(leftMouseButtonUp)
button.MouseButton1Down:Connect(leftMouseButtonDown)
MouseButton1Up
The MouseButton1Up event fires when the user releases their left Mouse up off of the GUI object.
This event is similar to GuiButton.MouseButton2Up, which behaves identically except that it is connected to the user's right mouse button.
If you are looking for an event requiring the user to press and release their left mouse on a GUI in order for the event to fire, consider using GuiButton.MouseButton1Click.
Note that this event will only fire for GUI buttons, including TextButtons and ImageButton. It will not fire for other GuiObjects.
Parameters
The mouse's x screen coordinate in pixels.
The mouse's y screen coordinate in pixels.
Code Samples
local button = script.Parent
local function leftMouseButtonUp(x, y)
print("Left mouse up at", x, y)
end
local function leftMouseButtonDown(x, y)
print("Left mouse down at", x, y)
end
button.MouseButton1Up:Connect(leftMouseButtonUp)
button.MouseButton1Down:Connect(leftMouseButtonDown)
MouseButton2Click
The MouseButton2Click event fires when the user's Mouse fully right clicks the GUI button.
By clicking, the mouse has to be in bounds of the button and has to be pressed down and up again before this event fires. If the mouse leaves the bounds of the button and is released, the event will not fire. If you would like to avoid this limitation, you can use GuiButton.MouseButton2Down and MouseButton2Up. These events are similar, but will fire whenever the user pressed their left mouse down or up, respectively.
This event is similar to GuiButton.MouseButton1Click, which behaves identically except that it is connected to the user's left mouse button.
Note that this event will only fire for GUI buttons, including TextButtons and ImageButton. It will not fire for other GuiObjects.
Code Samples
local button = script.Parent.Button
local function rightClick()
print("Right click pressed down and up on button.")
end
button.MouseButton2Click:Connect(rightClick)
MouseButton2Down
The MouseButton2Down event fires when the user presses their right Mouse button down on the GUI object.
This event is similar to GuiButton.MouseButton1Down, which behaves identically except that it is connected to the user's left mouse button.
If you are looking for an event requiring the user to press and release their right mouse on a GUI in order for the event to fire, consider using GuiButton.MouseButton2Click.
Note that this event will only fire for GUI buttons, including TextButtons and ImageButton. It will not fire for other GuiObjects.
Parameters
The mouse's x screen coordinate in pixels.
The mouse's y screen coordinate in pixels.
Code Samples
local gui = script.Parent
local button = gui.Button
function rightMouseButtonUp(x, y)
print("Right mouse up", x, y)
end
function rightMouseButtonDown(x, y)
print("Right mouse down", x, y)
end
button.MouseButton2Up:Connect(rightMouseButtonUp)
button.MouseButton2Down:Connect(rightMouseButtonDown)
MouseButton2Up
The MouseButton2Up event fires when the user releases their right Mouse up off of the GUI object.
This event is similar to GuiButton.MouseButton1Up, which behaves identically except that it is connected to the user's left mouse button.
If you are looking for an event requiring the user to press and release their right mouse on a GUI in order for the event to fire, consider using GuiButton.MouseButton2Click.
Note that this event will only fire for GUI buttons, including TextButtons and ImageButton. It will not fire for other GuiObjects.
Parameters
The mouse's x screen coordinate in pixels.
The mouse's y screen coordinate in pixels.
Code Samples
local gui = script.Parent
local button = gui.Button
function rightMouseButtonUp(x, y)
print("Right mouse up", x, y)
end
function rightMouseButtonDown(x, y)
print("Right mouse down", x, y)
end
button.MouseButton2Up:Connect(rightMouseButtonUp)
button.MouseButton2Down:Connect(rightMouseButtonDown)