ContextActionService custom button image is being reverted back to default despite being explicitly set

Hey, I want to create a ContextActionService button with a custom image.

Despite this being the correct way to do it, it still gets reverted back to the default image.

My code:

local function func()
	return
end

ContextActionService:BindAction("sprint", func, true,
	Enum.KeyCode.LeftShift
)


local actionButton:ImageButton=ContextActionService:GetButton("sprint")
actionButton.Image = "rbxassetid://8618269848"
actionButton.PressedImage = "rbxassetid://14112658774"
actionButton.ImageColor3 = Color3.fromRGB(255,255,255)
actionButton.Position = UDim2.new(1,-95,1,-170)
actionButton.Size = UDim2.fromOffset(70,70)

actionButton:FindFirstChildOfClass("TextLabel").Text = "Sprint"

RobloxStudioBeta_O1Gt1sTM6k

As you can see here, it only works the first time, then it gets reverted back to the default image for the button. The “PressedImage” seems to remain. Why???

1 Like

Some undocumented core script is maybe the cluprit?

Doing this after calling GetButton solves it, but it looks super dirty and ugly.

actionButton.Changed:Connect(function(property)
		actionButton.Image = "rbxassetid://8618269848"
		actionButton.PressedImage = "rbxassetid://14112658774"
	end)

quick note you should check if the button exists because it will throw an error if it doesnt exist (for pc players)
RobloxStudioBeta_Ucs701o1hn

anyway i dont think buttons are intended to be changed that way so my advice would be to make an imagelabel go over the top. ive got a really stupid solution for you

local ContextActionService = game:GetService("ContextActionService")

local function func()
	return
end

ContextActionService:BindAction("sprint", func, true,
	Enum.KeyCode.LeftShift
)


local actionButton: ImageButton = ContextActionService:GetButton("sprint")

if actionButton then
	actionButton.ImageColor3 = Color3.fromRGB(255,255,255)
	actionButton.Position = UDim2.new(1,-95,1,-170)
	actionButton.Size = UDim2.fromOffset(70,70)
	actionButton.ImageTransparency = 1
	
	local notpress = "rbxassetid://8618269848"
	local press = "rbxassetid://14112658774"
	
	local fake = Instance.new("ImageLabel")
	fake.Parent = actionButton
	fake.BackgroundTransparency = 1
	fake.Size = UDim2.fromScale(1, 1)
	fake.ScaleType = Enum.ScaleType.Fit
	fake.Image = notpress
	
	actionButton:GetPropertyChangedSignal("Image"):Connect(function()
		if actionButton.Image == "https://www.roblox.com/asset/?id=97166444" then
			fake.Image = notpress
		else
			fake.Image = press
		end
	end)

	local label = actionButton:FindFirstChildOfClass("TextLabel")
	label.ZIndex += 1
	label.Text = "Sprint"
end

If that’s not how you’re supposed to do it, then how did they accomplish it on this post?

They literally just set the 2 images and it worked for them?

1 Like

After lots of research, I’ve come to the conclusion that ContextActionService buttons are hard-coded to reset back to the legacy-looking default button and there is nothing you can do to change that.

The only way to get around it is by doing this after you bind your button:

actionButton.Changed:Connect(function()
		actionButton.Image = "rbxassetid://8618269848" -- Your image
		actionButton.PressedImage = "rbxassetid://14112658774" -- Your pressed image
	end)

The fact that this isn’t documented anywhere is insane!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.