In Creating a Score Bar, you built a basic GUI to display information. In this course, you’ll learn how to make on-screen buttons that can be used for menus, interface actions, and more. Show Button TypesThere are two types of button objects that can be used in your GUI design:
Creating a ButtonThe following steps show how to add an ImageButton to the screen and flip it between three appearances depending on the player’s interaction.
SizeFor the button to dynamically resize on various devices and screens, it’s best to use Scale properties.
ScalingThe current size should work nicely on a phone, but the X/Y scale of 0.15 (15%) may appear too large on a computer screen. To correct this, you can add a UISizeConstraint.
PositionButtons should typically be moved within a player’s thumb reach on mobile devices, for instance the lower-right area of the screen.
ImagesThis button needs three custom images — its normal appearance on the screen, a hover-over appearance, and a final image for when the player presses it.
Normal Hover Pressed Setting these appearances can be done through the Image, HoverImage, and PressedImage properties.
StylingTo finalize the button’s appearance on screen, make the following adjustments:
Button FunctionalityThe final task is hooking up basic button functionality.
This simple button script works as follows:
Although there are several different events which you can connect to buttons, the Activated event is the most reliable for basic buttons, providing standard button behavior on all platforms from PC to phone/tablet to console. If the button doesn’t work as expected, check the following:
This article expands on the articles/Using Images in GUIs|Using Images in GUIs tutorial and demonstrates how to make on-screen buttons that can be used for menus, in-game actions, and much more. Button TypesText ButtonA TextButton is very similar to a TextLabel, except that a player can activate it with a click/tap. Internally, it also shares many of the same visual properties as a text label — font, background color, stroke color, etc. Image ButtonSimilarly, an ImageButton is like an interactive version of the ImageLabel object and it uses a custom image that you upload to Roblox. It also shares most of the same properties as its non-button counterpart. Creating a ButtonThe remainder of this article demonstrates how to add an ImageButton to the screen and flip it between three appearances depending on the player’s interaction. Add an ImageButton
This will add an empty image button to the corner of the game view. Upload ImagesThis button needs three custom images — its normal appearance on the screen, a hover-over appearance, and a final image for when the player presses it.
ImgButtonNormal ImgButtonHover ImgButtonPressed You can use these menu buttons for testing — simply right-click each one and select Save Image As... to save them to a convenient location on your computer. Instead of uploading each image separately, use the Asset Manager to upload multiple assets at once:
Inside the folder, you should see the new images: Setting the three appearances for the button can be done through the ImageButton object.
Adjust the ButtonTo finalize the button’s appearance on screen, make the following adjustments:
Attach a LocalScriptThe final task is hooking up basic button functionality.
This is a basic button script and it works as follows:
Although there are several different event types which you can connect to buttons, the GuiButton/Activated|Activated event is the most reliable for basic buttons, providing standard button behavior on all platforms from PC to phone/tablet to console.
If the button doesn’t work as expected, check the following:
|