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ā.