Mobile Button Connected To an InputBegan Function

I need some help making a mobile gui button connect to a function inside of an InputBegan function. Heres my code:

local function onInput(input, process)
	if (process or not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
		return;
	end	
	
	if (input.KeyCode == Enum.KeyCode.E and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
--random stuff here
	elseif (input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.ButtonY and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
--random stuff here
	elseif (blank and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
--random stuff here
end

input.UserInputType == Enum.UserInputType.Touch
https://developer.roblox.com/en-us/api-reference/enum/UserInputType

1 Like

I tried that but it detects the entire screen, and I want a specific button. I put that code in, and then, even when I added for the gui button to be clicked, it still only detected everything around it besides the button itself.

https://developer.roblox.com/en-us/api-reference/event/GuiButton/Activated

Then explicitly listen to the button’s ā€˜Activated’ event/signal instead.

I tried doing that, and it does the same thing as detecting a mouseclick, detects the entire screen except for the button itself.

Since its a button why not use button.MouseButton1Click?

I’m not entirely sure what you’re doing wrong since what I’ve suggested has worked for me previously so all I can suggest is that you look into the ā€˜ContextActionService’.

https://developer.roblox.com/en-us/api-reference/class/ContextActionService

I already tried that, as seen in my last post.

It got too confusing I tried looking in to it already, if you could help with that that would be great.

https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction
Here’s an example with comments.

local Enumeration = Enum
local Game = game
local ContextActionService = Game:GetService("ContextActionService")

local function OnAction(ActionName, InputState, InputObject)
	if InputState == Enumeration.UserInputState.Begin then
		print("Hello world!")
	end
end

ContextActionService:BindAction("ActionName", OnAction, true) --Further arguments should be KeyCodes/UserInputTypes that the action is bound to.
--A value of 'true' as the third argument creates a 'ContextActionButton' for mobile users to interact with.

Could I put this into an already existing function? The function I want to use is a normal UIS function like:

local function onInput(input, process)

Whats wrong with that tho?

Chars limit

You should click on the link I provided for further documentation regarding ContextActionService:BindAction.

There are several ways to accomplish this without using ContextActionService. All Roblox GuiObjects (Frames, Buttons, ImageLabels, TextLabels, etc.) have InputBegan and InputEnded events that work pretty much the same way as UserInputService.InputBegan, UserInputService.InputChanged, and UserInputService.InputEnded do.

I have updated your existing onInput function to have an additional argument where you can pass a Button (or other GUI object) to the function.

Make sure that you replace Button with the path to your button


local function onInput(input, process, guiObject)
	if (process or not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then 
		return
	end	

	if (input.KeyCode == Enum.KeyCode.E and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
		--random stuff here	
	elseif (input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.ButtonY and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
		--random stuff here
	elseif (((input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1) and (guiObject ~= nil and guiObject == Button)) and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
		--random stuff here
	end
end

I also changed the second elseif from

elseif (blank and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then

To:

elseif (((input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1) and (guiObject ~= nil and guiObject == Button)) and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then

This makes sure that the code only runs if the button is clicked and not when you click or tap anywhere else on the screen.

Now we need to detect when mouse or touch input has began on the button and call the onInput function:

Button.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then
		onInput(input, false, Button)
	end
end)

The InputBegan event for GuiObjects does not have the processed argument so we have to pass false as the second argument in the function. Then, you will pass the button as the third argument in the function.

Remember to set Button to the path to your button.

Also, here is the script that I used to test my solution:

Script
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local PlayerGui = Player:WaitForChild("PlayerGui")
local TestGui = PlayerGui:WaitForChild("TestGui")
local Button = TestGui:WaitForChild("Button")

local Character = Player.Character
local humanoid = Character:WaitForChild("Humanoid")

local function onInput(input, process, guiObject)
	if (process or not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then 
		return
	end	

	if (input.KeyCode == Enum.KeyCode.E and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
		print("E key pressed")	
	elseif (input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.ButtonY and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
		print("Y button on gamepad pressed")
	elseif (((input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1) and (guiObject ~= nil and guiObject == Button)) and humanoid:GetState() == Enum.HumanoidStateType.Freefall) then
		print("Mobile button pressed")
	end
end

UserInputService.InputBegan:Connect(onInput)
Button.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then
		onInput(input, false, Button)
	end
end)

I tested this in a blank Baseplate in Studio, the code above was placed in a LocalScript in StarterPlayerScripts. I also created a ScreenGui named ā€œTestGuiā€ in StarterGui and a TextButton named ā€œButtonā€ inside of ā€œTestGuiā€.

tysm you fixed the issue! i didn’t think it could be fixed after forummer couldn’t do it lol.

1 Like