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"
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???
quick note you should check if the button exists because it will throw an error if it doesnt exist (for pc players)
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
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!