How to change the button in ContextActionService

Hi guys,

So I recently found that you can create this button with ContextActionService


You might think this is impossible, but it actually works

You might need:

  1. the modern button
  2. the icon (optional)

How to do it:

  1. Create a basic code for ContextActionService
local function action1(actionName, inputState)
	if actionName == "action1" then
		if inputState == Enum.UserInputState.Begin then
          --// your code runs here
		elseif inputState == Enum.UserInputState.End then
          --// your code runs here
		end
	end
	
end

ContextActionService:BindAction("action1", action1, true, Enum.Keycode.A) --// change the Enum.Keycode.A with your choice
ContextActionService:SetPosition("action1", UDim2.new(0, 75, 0, 20)) --// put custom position instead this
  1. Get the button imageID, mine is:
rbxassetid://97111545264801 - normal
rbxassetid://70702683022015 - clicked
  1. Insert the image id like this:
local function action1(actionName, inputState)
	if actionName == "action1" then
		if inputState == Enum.UserInputState.Begin then
			ContextActionService:GetButton("action1").Image = "rbxassetid://70702683022015"
			--// your code runs here
		elseif inputState == Enum.UserInputState.End then
			ContextActionService:GetButton("action1").Image = "rbxassetid://97111545264801"
			--// your code runs here
		end
	end
end

ContextActionService:GetButton("action1").Image = "rbxassetid://97111545264801"

So the final code looks like this:

local ContextActionService = game.ContextActionService

local function action1(actionName, inputState)
	if actionName == "action1" then
		if inputState == Enum.UserInputState.Begin then
			ContextActionService:GetButton("action1").Image = "rbxassetid://70702683022015"
			--// your code runs here
		elseif inputState == Enum.UserInputState.End then
			ContextActionService:GetButton("action1").Image = "rbxassetid://97111545264801"
			--// your code runs here
		end
	end
	
end

ContextActionService:BindAction("action1", action1, true, Enum.UserInputType.MouseButton1)
ContextActionService:SetPosition("action1", UDim2.new(0, 75, 0, 20))
ContextActionService:GetButton("action1").Image = "rbxassetid://97111545264801"
ContextActionService:SetImage("action1", "rbxassetid://10455604823")

Proof:

Pretty sure with the new way of handling input roblox is deprecating ContextActionService

2 Likes