NubHud - A CUSTOMIZABLE MOBILE HUD

INTRODUCTION

NubHud is a module in which you can create customizable buttons for touch-enabled devices.

It works in a simple way, allowing you to create, edit, and delete buttons with just a few lines of code.

Why Use this?

Unlike the conventional buttons in Roblox, created with Context Action Service, the buttons created with NubHud can be almost fully customized, allowing you to create a more attractive mobile HUD for players. The module already comes with editing functions, enabling the player to edit the position and size of each button.

ACCESS

You can get it from Roblox Marketplace

API

Methods

NubHud.Enable()

Enable all buttons visibility

NubHud.Disable()

Disable all buttons visibility

NubHud.new(ButtonName : string) : Button

Create a new button

Button:GetConfigs() : table

Return a table with button Position and Size

Button:SetLabel(Text : string)

Set button label

Button:SetImage(Image : string)

Set button image

Button:SetSize(Size : UDim2)

Set button size

Button:SetPosition(Position : UDim2)

Set button position

Button:BindToFunction(BindName : string, Function)

Bind button events to a function

Button:UnbindFunction(BindName : string)

Unbind the function

Button:Destroy()

Destroy the button.

Events

NubHud.HudChanged : table

Fires when the HUD is modified by the player
Return all buttons configs

Button.TouchStarted

Fires when player starts pressing the button
Return button name and input state

Button.TouchEnded

Fires when player stop pressing the button
Return button name and input state
Properties

Button.IdleImageId

Button Idle Image

Button.PressedImageId

Button Pressed Image

Button.Locked

Defines whether the player can edit the button

Examples

Creating a button

local UserInputService = game:GetService("UserInputService")

if UserInputService.TouchEnabled then
	local NubHud = require(game.ReplicatedStorage.NubHud)

	local ExampleButton = NubHud.new("Example Button")
end

By simply doing this, you will already have a button that can be used, with the default HUD settings.

It is not recommended to require the module if the player doesn’t have touch enabled, as the HUD is already created when you require it.

Setting up the button

By default, the button will appear in the center of the screen with a size of 50 pixels and without an image or text. You can edit the button using some simple methods.

local UserInputService = game:GetService("UserInputService")

if UserInputService.TouchEnabled then
	local NubHud = require(game.ReplicatedStorage.NubHud)

	local ExampleButton = NubHud.new("Example Button")
	ExampleButton:SetLabel("Example button")
	ExampleButton:SetPosition(UDim2.new(0.8, 0, 0.375, 0))
	ExampleButton:SetSize(UDim2.new(0, 100, 0, 100))
	
	local ExampleButton2 = NubHud.new("Example Button 2")
	ExampleButton2:SetImage("http://www.roblox.com/asset/?id=9011713759")
	ExampleButton2:SetPosition(UDim2.new(0.9, 0, 0.3, 0))
end

Using the button events

The button has 2 events that can be used to detect when the player presses and releases the button, as well as a method that allows you to connect both events directly to a function.

local UserInputService = game:GetService("UserInputService")

local function OnButtonEvent(ButtonName, InputState, BindName)
	print(`{ButtonName} touch {InputState.Name}`)
end

local function OnTouchStarted(ButtonName, InputState)
	print(`{ButtonName} touch started`)
end

local function OnTouchEnded(ButtonName, InputState)
	print(`{ButtonName} touch ended`)
end

if UserInputService.TouchEnabled then
	local NubHud = require(game.ReplicatedStorage.NubHud)

	local ExampleButton = NubHud.new("Example Button")
	ExampleButton:SetLabel("Example button")
	ExampleButton:SetPosition(UDim2.new(0.8, 0, 0.375, 0))
	ExampleButton:SetSize(UDim2.new(0, 100, 0, 100))
	
	ExampleButton.TouchStarted:Connect(OnTouchStarted)
	ExampleButton.TouchEnded:Connect(OnTouchEnded)
	
	ExampleButton:BindToFunction("Example Bind", OnButtonEvent)
end

There are other things in the API that I didn’t explain the usage of in the examples, as they are for more specific cases.

Any bug reports, please comment here so I can resolve them as quickly as possible. :heart:

59 Likes

this module is very good, you’re the goat, akanub! :heart_eyes:

2 Likes

THIS IS SO FIRE!

Thank you so much, I will be using this extensively.

I will definitely will be using this! Great module :white_check_mark::white_check_mark:

This is a great module, the buttons created by CAS look gray and outdated (and also don’t fit with the new buttons).

Where method chaining bruhhhhhhhhhhhh

I will for sure be using this module. Great stuff, keep it up! :coefficients: :coefficients: :coefficients:

How do I connect to a button click? I don’t really know how to use the InputState Enum, sorry

it looks like for a full click/tap you’d want to use TouchEnded

TouchEnded seems to trigger even if I am not hovering over the button when I lift my finger

yeah it doesn’t look like the dev provided anyway to listen for an actual click after looking at the code. try checking if the button is at the finger position inside the TouchEnded callback by using PlayerGui:GetGuiObjectsAtPosition

1 Like

QOL UPDATE

Since the last update, there have been some quality-of-life improvements, with some methods removed and new ones added. So before replacing your old version, make sure to check what has changed:

Changes

Some bug fixes

Now you can require the module without needing to be on a device with touch enabled, but you won’t have access to the HUD

If you were using the previous method:

module.GetButtons()

it’s not recommended to update the module unless you’re prepared to rewrite your code

The method for scaling the button has changed. Previously, you would enter a number in a box; now it works with a slider. Unlike the old version, this new version uses Scale to ensure that button sizes remain consistent across different screen sizes

You can change the minimum and maximum size of the buttons through the module’s code; by default, it ranges from 0.05 to 0.2

New methods have been added so that you can get and create a HUD as a whole in a more practical way

API

Methods

module.GetHud() -> Hud

Returns a table with Hud
information such as button positions, etc

module.ApplyHud(Hud)

Creates buttons according to the Hud
Returns a table with the created buttons

If a HUD already exists, this method will clear it and create a new one

Example

  • A simple system where you can save your Hud so that every time the player joins the game, they don’t have to customize it again

Client-Side

local MobileHud = require(game.ReplicatedStorage.NubHud)
local MobileHudEvent = game.ReplicatedStorage.NubHudEvent

local function OnMobileHudEvent(NewHud)
	MobileHud.ApplyHud(NewHud)
end

local function OnHudChanged(NewHud)
	MobileHudEvent:FireServer(NewHud)
end

local DefaultHud = {
	{
		Name = "Button 1",
		Image = "",
		Label = "Button 1",
		Position = {
			XOffset = 0,
			YOffset = 0,
			XScale = .2,
			YScale = .7,
		}
	},

	{
		Name = "Button 2",
		Label = "Button 2",
		Image = "",
		Position = {
			XOffset = 0,
			YOffset = 0,
			XScale = .4,
			YScale = .7,
		}
	},

	{
		Name = "Button 3",
		Label = "Button 3",
		Image = "",
		
		Position = {
			XOffset = 0,
			YOffset = 0,
			XScale = .6,
			YScale = .7,
		}
	},

	{ 
		Name = "Something",
		Label = "Button 4",
		Image = "",
		Position = {
			XOffset = 0,
			YOffset = 0,
			XScale = .8,
			YScale = .7,
		}
	},
}

MobileHud.ApplyHud(DefaultHud)

MobileHud.HudChanged:Connect(OnHudChanged)
MobileHudEvent.OnClientEvent:Connect(OnMobileHudEvent)

This way, when the game starts, the HUD will be created based on the default HUD. When it’s modified, the new HUD will be sent to the server to be saved. If a saved HUD already exists, the server will send it to the client and replace the default HUD with the previously saved one.

Server-Side

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local MobileHudEvent = game.ReplicatedStorage.NubHudEvent

local PlayersHuds = DataStoreService:GetDataStore("PlayersLayouts")

local function OnMobileHudEvent(Player, NewHud)
	local Success, Result = pcall(function()
		return PlayersHuds:SetAsync(Player.UserId, NewHud)
	end)

	if Success then
		warn("Success 😀")
	else
		warn("Problem 😭")
	end
end

local function OnPlayerAdded(Player)
	local Success, Result = pcall(function()
		return PlayersHuds:GetAsync(Player.UserId)
	end)

	if Result then
		MobileHudEvent:FireClient(Player, Result)
	else
		warn("Very Bad Problem 😭")
	end
end

MobileHudEvent.OnServerEvent:Connect(OnMobileHudEvent)
Players.PlayerAdded:Connect(OnPlayerAdded)

You’re the GOAT! Damn, you almost killed my ears with that Lady Gaga music.

Very nice, will likely use this in my climbing game. Thanks so much, I’ll be sure to leave a credit.

seems promising here, wish i could try a demo of it without having to set one myself, also are these buttons behavior relative to default roblox buttons or CAS???

oftentimes i tend to avoid using mobile controls tool that had this issue cuz the behavior of it doesn’t work perfectly like how mobile games outside roblox does, it often gets stuck on whatever state the code is running when the tap pos is outside

especially using it for smth like fire buttons which doesn’t fit at all

I like this a lot but I made some few tweaks to it made the buttons have a blue tint when you are in edit mode and moved the lock button to the topbar using topbar plus I don’t want to my players to accidentally hit the lock button when using the joystick

well


it’s the same way as ContextActionUtility and jump button, guess i’ll have to fork this but still good tho

Really op, saved me a lot of time. Thank you once again AkaNub!

1 Like