So, I’m trying to make a button that has both PC and mobile compatibility that opens a panel.
However, I need it to be specifically as this:
For the PC, it can be operated by a key press
For mobile, it can be operated by pressing the button
I know how to make both individually, but how could I make both of these things work with a same function without duplicating the code countless amount of times? I’ve seen some developers that pulled this off.
Make your function a global variable, and then call it at the end of the script using UserInputService or ContextActionservice. this is the only way as I’m aware.
I checked it out and here’s what I came up with so far, but how do I also incorporate the mobile action?
game:GetService("UserInputService").InputBegan:connect(function(input, Processed)
if not Processed then
if input.KeyCode == Enum.KeyCode.R then --a PC key was pressed
print("R has been pressed")
elseif input.UserInputType == Enum.UserInputType.Touch then
--something was touched on a touchscreen, but how do I get the button
end
end
end)
I dont think you can get the specific button that was pressed from any script.
Your best bet would be this:
game:GetService("UserInputService").InputBegan:connect(function(input, Processed)
if not Processed then
if input.KeyCode == Enum.KeyCode.R then --a PC key was pressed
print("R has been pressed")
someFunction()
end
end
end)
script.Parent.Button.MouseButton1Click:Connect(function()
if game:GetService("UserInputService").TouchEnabled == true then
someFunction()
end
end)
A lot of major developers rely on UserInputService for mobile support, so I feel like it’s the best approach using UserInputService. I think it exists.
game:GetService("UserInputService").InputBegan:connect(function(input, Processed)
if not Processed then
if input.KeyCode == Enum.KeyCode.R then --a PC key was pressed
print("R has been pressed")
someFunction()
end
end
end)
Frame.InputBegan:connect(function(inputObj)
if inputObj.UserInputType == Enum.UserInputType.Touch then --a mobile button was touched
print("Gui has been touched!")
someFunction()
end
end)