Help needed with Mobile 'KeyCode' function support

Hello, not even 2 hours later and I’m back haha. Anyone know how to do a KeyCode function, but for Mobile users? I’ve been looking around not too many good tutorials, lol. Suggestions below. Here is the code:

local PressE = game.Workspace.PressE
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local root = character:WaitForChild("HumanoidRootPart")

UIS.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.E then
		if (PressE.Position - root.Position).Magnitude < 12 then
			player.PlayerGui.ScreenGui.Enabled = true
		end
	end
end)
2 Likes

I dont fully understand what youre trying to accomplish. What “Key” is there for a mobile user to press? The only thing i can think of is make the button able to enable the screengui when pressed.

1 Like

Sorry forgot to mention, yes that is what I am talking about.

1 Like

Since there is no “Key” on mobile to press, to open a GUI, I would just make a Button that opens the GUI. In that case here is a post that explains that:

More specifically: How would you go about making a gui open and close? - #6 by baitem

Hope this helped.

1 Like

Oh, I’ve already figured that out just wondering how I can make the open/close on mobile work.

For a button, you can use Activated event, which works both for pc and mobile if im not mistaken

button.Activated:Connect(function()
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Enabled = true
end
1 Like

Is button a variable for ur script?

Yes, you can index the button using local. The local script that does this should be in the button itself. Im assuming youre dealing with a BillboardGui. It might act differently but i dont know, i dont fiddle with BillboardGuis that often

1 Like

For that script is it for opening only? Sorry I’m a noobie. :joy:

Activated isn’t a valid member of Frame.

Yup, if you want it to open and close, then you can do this:

local button = script.Parent
local screengui = game.Players.LocalPlayer.PlayerGui.ScreenGui

button.Activated:Connect(function()
    if screengui.Enabled == true then
        screengui.Enabled = false -- closes gui if it is open
    else
        screengui.Enabled = true -- else, opens it
    end
end)

Hope you understand. Happy new year :partying_face:

1 Like

Activated event cannot be used for a Frame instance. You need to use a TextButton/ImageButton

1 Like

Tysm, but what if u have a frame attached to the GUI too?

Just insert a TextButton, place / size it over the Frame, and then make it invisible.

I reccomend inserting the button inside the Frame, Sizing it to {1,0}.{1,0} and then setting BackgroundTransparency to 1 and Text to blank. Now the frame should look the same, but act as a button. But when making the script, make sure you index the button, not the frame!

1 Like

Okay, Tysm for the help! Have a happy new year! :partying_face:

1 Like

i got one better for you

local button = script.Parent
local screengui = game.Players.LocalPlayer.PlayerGui.ScreenGui

button.Activated:Connect(function()
        screengui.Enabled = not  screengui.Enabled -- closes gui if it is open
end)
4 Likes

I just realized, I actually have a billboard attached which when u press E it opens and closes the GUI so I don’t don’t know what to do now. What I’m trying to achieve is a button to open a GUI on mobile, Ive already got one for desktops.

I suggest making a GUI in StarterGui which when you press will open the BillboardGui, the only downside would be of course the KeyCode you made will be useless as there is not really an official way to make the GUI for mobile users only if I’m correct.

1 Like

So if i’m reading you correctly, you want to make a billboard gui visible when it’s tapped on mobile, OR E is pressed on PC?

in that case you could do something like…

local button = script.Parent
local screengui = [wherever the billboardgui is]



KeyBindings = {}
KeyBindings['E'] = function()  screengui.Enabled = not  screengui.Enabled end

UIS.InputBegan:Connect(function(input, gpe)
if KeyBindings[Input.Keycode.Name] and not gpe then
	KeyBindings[Input.Keycode.Name]()
end)
button.Activated:Connect(function()
        KeyBindings['E'] ()
end)

you can sort of by checking if there’s touchscreen, and if there’s an accelerometer, as all mobile devices have that, but i’m pretty sure no touchscreen monitors do
sadly there’s no IsOnMobile API

2 Likes

The Billboard GUI is in the part where you press E. My goal here is I want the open/close to work on mobile since I’ve already gotten a working one on PC, sorry for the confusion.

1 Like