Hello, I am wondering how can I make a GUI button that can force a keycode. For example, if I made a textbutton called crouch and click on it I want it to do crouch but with a KeyCode on pc "c".
[SOLVED]How can I make a gui button that forces a keycode on Roblox?
3 answers
Ever heard of GUIBUTTON.TouchTap? Yeah, you maybe want to read it on the official dev site of ROBLOX. Here's some sample code (in a localscript):
myButton = script.Parent
myButton.TouchTap:Connect(function()
print('Tap')
end)
Also, if you're looking for the way Arsenal makes it's buttons, take a look at ContextActionService on the dev site "developer.roblox.com" or search up some Youtube tutorial. That can help a lot.
Here's some sample code of ContextActionService (localscript):
ContextActionService = game:GetService('ContextActionService')
function onButtonPress()
if workspace:FindFirstChild('Baseplate') then
workspace.Baseplate:Destroy()
end
end
-- Arguments: Name of action, Function that will run when the action happens, Do you want the button to show up?, If on PC, what key would trigger this same event?
mobileButton = ContextActionService:BindAction("DeleteButton", onButtonPress, true, Enum.KeyCode.R)
-- Setting position.
ContextActionService:SetPosition("DeleteButton", UDim2.new(0.72, -25, .2, -25))
-- How to set an image onto the button if you don't want text on it.
ContextActionService:SetImage("DeleteButton", "rbxassetid://SOMEROBLOXIMAGEID")
There is already an explanation.
If source did not work, use this: https://scriptinghelpers.org/questions/5124/can-i-create-custom-roblox-keybinds
I have a better idea actually,
You need to make a BoolValue and Name it "Crouch"
When Gui Button or Keybind pressed, you can just change the value from "Crouch" to true and it will make player crouch!
Hope It Helps
This discussion is preserved as it was. New answers, comments, and votes are closed.

7 comments
Not what I'm looking for.
what I'm looking for is a way to make a gui textbutton become a KeyCode.
Like in arsenal mobile when you press the crouch icon you crouch without pressing "c" like you would have to do on Pc.
What do you mean? Can you explain a little more details please?
For example if I made a gun and in order to reload it you need to press "R" on Pc, How can I make it so that if I am on mobile I can click a button on screen to make it seem like I pressed "R".
Make a script detects are you mobile, if you are, visible the button, and if hes a pc player, load a script for him to press r, like you can use if statements to give the following codes for them
I will try to make a code for you