AutoButtonColor for ImageButtons

Usually, I use ImageButtons with a completely transparent background. AutoButtonColor unfortunately only affects the background of the button, and not the actual image, which is what I need. I havent seen anyone else do this, and since I needed it, I made this pretty simple script.

Demo:

Script: (LocalScript in StarterPlayerScripts)
local collectionService = game:GetService("CollectionService")


function registerImageButtonColor(button: ImageButton): ()
	if button:IsA("ImageButton") then
		local stateFrame = Instance.new("ImageLabel")
		stateFrame.Name = "StateFrame"
		stateFrame.Size = UDim2.new(1, 0, 1, 0)
		stateFrame.BackgroundTransparency = 1
		stateFrame.ImageTransparency = 0.7
		stateFrame.Image = button.Image

		button:GetPropertyChangedSignal("GuiState"):Connect(function()
			if button.GuiState == Enum.GuiState.Hover then
				stateFrame.ImageColor3 = Color3.fromRGB(0, 0, 0)
				stateFrame.Parent = button
			elseif button.GuiState == Enum.GuiState.Press then
				stateFrame.ImageColor3 = Color3.fromRGB(255, 255, 255)
				stateFrame.Parent = button
			else
				stateFrame.Parent = nil
			end
		end)
	else
		warn(button, "is not a valid ImageButton!")
	end
end


for _, button: GuiButton in collectionService:GetTagged("ImageButtonStateColor") do
	registerImageButtonColor(button)
end


collectionService:GetInstanceAddedSignal("ImageButtonStateColor"):Connect(registerImageButtonColor)

How to use:
Just add a “ImageButtonStateColor” tag to any ImageButton to make it work.

Feel free to find a better name for the tag too lol

Also keep in mind that this might not work properly for images that were uploaded in a different color than white. The demo uses a white image with ImageColor3 set to 255, 0, 0. Making this work with black or otherwise dark images is impossible (or at least to my knowledge), so make a feature request instead if thats what you are looking for.

7 Likes