Keypress GUI for mobile players

  1. I’m trying to make a small button that presses the key E. (I’m a super beginner scripter so the simplest option/a script to help would be AMAZING)

It would also be nice if it only appeared for mobile players.

  1. I have no clue how to use enum or cas or whatever

  2. I looked all over the developer hub, but I couldn’t follow along with anything I saw because I have little scripting knowledge.

Thank’s for the help I know I’ll get!

2 Likes

Hi,

I think you’re right that ContextActionService is probably the best way to go with this

You need a function that will be called when someone presses E or clicks your button

local function clickHandler(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
– do whatever it is you want here
end
end

Then here CaS makes sure this is called when someone presses E. The “true” here says to add a button for mobile players.

ContextActionService:BindAction(“Interact”, clickHandler, true, Enum.KeyCode.E)

This looks big-brainy so I’m gonna test it in a few, thanks! I’ll tell you if it works.

I would recommend using a proximity prompt as it’s put in already for mobile!

I’m trying to make it so it literally presses e. It could work in multiple instances, as I use a grappling hook sometimes or sometimes interacting with NPCS. the part where it says -do whatever it is you want here wouldn’t work because I need it to literally press E

1 Like

no you don’t, you need to do what would normally happened if you press e, you can’t press e or make them press a button on roblox

hmm, okay. I guess I didn’t know enough about scripting to know that. Thanks for your help and I’ll try and figure this out.

1 Like

I’m confused on where I would put this. Would it be in a gui i make, in a gui with a text button, or just in playerGUI?

If you are using CAS then just in a localscript, so not attached to any button. You can add it to a script thats in PlayerGui, that would work

So I’ve got this script added here
image

local ContextActionService = game:GetService("ContextActionService")

local function clickHandler(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		print("Something happens here")
	end
end


ContextActionService:BindAction("Interact", clickHandler, true, Enum.KeyCode.E)

That adds the button you can see here, and when you click that button on mobile, or press E on keyboard it will trigger the “something happens here” to be printed. Of course you can then put whatever code you need there instead.

If you want to change how the button looks you can put an image on it pretty easily, use after you’ve called BindAction. Just make sure that the name here matches, I’ve called it “Interact”, it can be whatever you want, just use the same in both places
ContextActionService:SetImage(“Interact”, “rbxassetid://6680731071”)

If you want more control over it then you can use
local interactButton = ContextActionService:GetButton(“Interact”)
This gives you the button and you can add some code to move it, change its size, or whatever else you need.

All that would go in the same LocalScript

Hope that helps,

6 Likes

This is amazing, it works perfectly, I was just wondering if could move it so it isn’t smushed into the jump button on my phone?

Hi,

Glad it works!

ContextActionService:SetPosition(“Interact”, UDim2.new(0.4, 0, 0, 0))
You’d need to play around with the position here to get it in the right place

If you want more control over the button then you can use
local interactButton = ContextActionService:GetButton(“Interact”)
this gives you the button that was created and you can change its size, position, colour, or whatever else you want

1 Like

Thank you SO much!!! This is amazing!

1 Like