Binding and unbinding mobile buttons

I just wanna know if the correct way to turn on and off a mobile button I created is to unbind it, then recreate it again to enable it?

-- Create button
ContextActionService:BindAction(
	'Sprint',
	SprintFunction,
	true,
	Enum.KeyCode.LeftShift,
	Enum.KeyCode.ButtonR2
)
		
ContextActionService:SetImage('Sprint', 'rbxassetid://4654541506')
ContextActionService:SetPosition('Sprint', UDim2.new(1, -80, 1, -150))

-- Clear the button
ContextActionService:UnbindAction('Sprint')

Just feels a little wrong to be constantly binding and then unbinding the button. I was hoping there was a way to just make the button invisible? and then make it re-appear?

I believe you can use GetButton to retrieve the ImageButton associated with the action, and set its Visible property from there.

Doing something like this should work:

-- Create button
ContextActionService:BindAction(
	'Sprint',
	SprintFunction,
	true,
	Enum.KeyCode.LeftShift,
	Enum.KeyCode.ButtonR2
)
		
ContextActionService:SetImage('Sprint', 'rbxassetid://4654541506')
ContextActionService:SetPosition('Sprint', UDim2.new(1, -80, 1, -150))

local SprintButton = ContextActionService:GetButton('Sprint')

SprintButton.Visible = false

Note that the function will return nil if the device is not mobile, so make sure to do proper checks before setting the visibility status.

1 Like