Help me bind mobile button please?

  1. I’m trying to make a mobile button, when they click it, it replicates what happens when they player presses F

  2. I don’t know how to do this, because all the tutorials I’ve read I have to state the defined function, though there’s not really any.

  3. I’ve tried reading on the developer hub, and watching videos, (like one made by @Alvin_Blox)
    heres my script.
    https://gyazo.com/8c12b4dbae8272f8b5bc9f94e0830357
    heres my attempt at making it work.

It may seem easy to other scripters, but I’m extremely new to coding, and its a bit confusing. The first picture was acting weird, so i just made it a link. Pease dont steal the script. :stuck_out_tongue: thanks for reading!

4 Likes

Typically when it comes to accounting for mobile buttons, there’s the ContextActionService. Its BindAction methods come with a built-in way to create mobile buttons and bind touch taps on them to the function that you pass to BindAction. You can also have other input types call the function.

As far as a mobile button goes while using UserInputService, you’ll have to apply somewhat of the same paradigm and ensure that your states are in order. Typically for UserInputService, you disregard an actual button and relate touch taps anywhere on the screen to the function you’re binding.

4 Likes

So it looks like you want to create an on screen button using ContextActionService, and more specifically, the BindAction function. This function can be used to process keyboard input, gamepad input, and to create an on screen mobile button.

image


I suggest you re-work your code to ensure you’ve written it all and understand it thoroughly; it will help you become a better programmer. I will be referencing some of the code code you’ve provided in my explanation, as well as the DevHub.

The following explanation will assume your knowledge of functions and parameters. If you don’t understand them, reference the DevHub for more information.


Firstly, you’ll want to define a specific function for attacking, so it can be referenced more easily.

Assuming the code inside of your UserInputService InputBegan function is what you want to happen when you attack, this is what it should look like. Again, this is assuming your code for attacking is already functional.

local function Attack()
	if canattack == true then
		canattack = false
		event:FireServer()
		script.RemoteEvent:FireServer(AttackArm)
		human:LoadAnimation(AttachArm and script.R or script.L):Play()
		wait(0.6)

		AttackArm = not AttackArm
		canattack = true
	end
end

Now that we have the Attack function defined, we’ll take a look at BindAction.

There are 4 required parameters for the function BindAction, which are the following:

The second parameter for BindAction must be a Function, and this function will be automatically called when the specified inputTypes are pressed. For instance, when you press the key F. This is not the Attack function though; this is a function that will process the input, then call the Attack function afterward.

functionToBind, when called, will be passed 3 parameters, which are the following:

With this in mind, we can define another function to uniformly handle processing inputs, and from there, call the Attack function. The function that will be responsible for all this will be called ProcessAction, and it will receive the three aforementioned parameters.

local function ProcessAction(ActionName,InputState,InputObject)
	-- This function ProcessAction will be called both when the key is pressed, and
	-- when it is released.
	-- We want to make sure this only proceeds if the key is pressed, and not released.
	if InputState == Enum.UserInputState.Begin then
		-- If the ActionName is correct, proceed and call the Attack function.
		if ActionName == "Attack" then
			Attack()
		end
	end
end

And all that is left to do now, is to call BindAction.
However, make sure ContextActionService is defined somewhere in your code above this, as so:

local ContextActionService = game:GetService("ContextActionService")
-- The third parameter specifies whether there will be an on screen button for Mobile.
-- And all the parameters after that are the additional sources of input (gamepad, keyboard, etc.).
ContextActionService:BindAction("Attack", ProcessAction, true, Enum.KeyCode.F, Enum.KeyCode.ButtonY)

This will internally bind an action by the name of “Attack”, to a mobile button, the keyboard key F, and the gamepad button Y. When any of those inputs are triggered, the function ProcessAction will be called to process the action.

Here’s what it’ll look like so far:

This is a default button though, with no text or image, and it isn’t in the best position. This can be corrected using the functions SetPosition and SetTitle.

ContextActionService:SetPosition("Attack",UDim2.new(0,0,0,0))
ContextActionService:SetTitle("Attack","Attack")

When these changes are made, the button looks more presentable, and even says “Attack” on it. This is not all though, as further customizations can be made (see SetDescription, SetImage, GetButton).


And all together your final code product may look something like this:

local ContextActionService = game:GetService("ContextActionService")

local function Attack()
	-- Attack code
end

local function ProcessAction(ActionName,InputState,InputObject)
	-- This function ProcessAction will be called both when the key is pressed, and
	-- when it is released.
	-- We want to make sure this only proceeds if the key is pressed, and not released.
	if InputState == Enum.UserInputState.Begin then
		-- If the ActionName is correct, proceed and call the Attack function.
		if ActionName == "Attack" then
			Attack()
		end
	end
end

-- The third parameter specifies whether there will be an on screen button for Mobile.
-- And all the parameters after that are the additional sources of input.
ContextActionService:BindAction("Attack", ProcessAction, true, Enum.KeyCode.F, Enum.KeyCode.ButtonY)

ContextActionService:SetPosition("Attack",UDim2.new(0,0,0,0))
ContextActionService:SetTitle("Attack","Attack")
14 Likes

Definitely agree. I bind through ContextActionService. Some people do it through mouse events but, again, I don’t see the purpose for that in the long run. +1

Thanks a lot! :joy: I didn’t write the punching script, but i did the fireevent one, thanks for helping me out, it means alot to me! :stuck_out_tongue:

2 Likes