Help with making a context action service button equal to a keyboard input

Hello fellow developers ! I want to make a context action service button for my game which will have the same function of a shift button. This is the code I wrote so far.

local contextActionService = game:GetService("ContextActionService")
function onButtonPress()
	--HELP IS NEEDED FOR THIS AREA
end
local mobilebutton = contextActionService:BindAction("RunButton",onButtonPress,true,"Shift")
contextActionService:SetPosition("RunButton",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("RunButton","http://www.roblox.com/asset/?id=65455156")

I was successful in creating the button but what should I write after ‘function on ButtonPress()’ so that the same thing happens which happens when someone clicks shift on keyboard. I will really appreciate any help I receive.

1 Like

Hello!

The easy way to do this is to just fire a RemoteEvent to the server saying the Player is holding or releasing the shift button!

local contextActionService = game:GetService("ContextActionService")
local sprintEvent = game:GetService("ReplicatedStorage"):FindFirstChild("Sprint")
function onButtonPress(actionName, inputState, inputObject)
	sprintEvent:FireServer(inputState == Enum.UserInputState.Begin) --Fire a boolean to the server.
	--inputState == Enum.UserInputState.Begin is a boolean checking if inputState is the same as the enum for the beginning of an input.
	--So, if our input state is the start we fire true (Starting our sprint) otherwise, we know to cancel it.
end
--Removed the local variable "mobileButton" as BindAction doesn't return anything
contextActionService:BindAction("RunButton",onButtonPress,true,Enum.KeyCode.Shift)
contextActionService:SetPosition("RunButton",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("RunButton","http://www.roblox.com/asset/?id=65455156")
2 Likes

Hello ! Thank you so much for this but even when I used your script , the button is still not working. I have linked the shift key for sprinting and I want that same thing to happen. I created a remote event in replicated storage name ‘sprint’ and this is the script I used.

local contextActionService = game:GetService("ContextActionService")
local sprintEvent = game:GetService("ReplicatedStorage"):FindFirstChild("Sprint")
function onButtonPress(actionName, inputState, inputObject)
	sprintEvent:FireServer(inputState == Enum.UserInputState.Begin)
end
local mobilebutton = contextActionService:BindAction("RunButton",onButtonPress,true,"Shift")
contextActionService:SetPosition("RunButton",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("RunButton","http://www.roblox.com/asset/?id=65455156")