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