I'm trying to change the ImageTransparency on ImageLabels with the same ImageColor3

So, I recently found a solution to my color changing problem with ImageLabels. No disrespect to anyone who gave their suggestions, but I’m not an expert scripter, so I couldn’t figure it out for the life of me. Anyway, I am now trying to find a way to do the following: highlight ImageLabels that have the same ImageColor3 and have them change ImageTransparencies at the same time, via a GUI panel.

Going back to Blockbusters, whenever a team was close to finishing a path, their appropriate colors would flash on the game board. The same applied when they made a complete path from one side to the other. The cheap alternative would be to flash the white and blue spaces on the outside of the board, but I want to try the former before doing the latter.

As before, the panel would be connected to three other screens (parts with SurfaceGuis) that display the game board.

local screenGui = script.Parent

for _, image in ipairs(screenGui:GetDescendants()) do --change to GetChildren() if necessary
	if image:IsA("ImageLabel") then
		if image.ImageColor3 == Color3.new(0, 0, 0) then --black for example
			image.Transparency = 1 --make invisible
		end
	end
end

It does work, but not in the way I envisioned it. I have a TextButton and when I click that button, I want the script inside it to activate the ImageLabel transparencies for those with the same colors. However, they need to work with the ImageLabels that already turned that color, not the ones with them preset. I have a panel that changes the hexagon’s colors.

local gui = script:FindFirstAncestorWhichIsA("ScreenGui")
local button = script.Parent

button.MouseButton1Click:Connect(function()
	for _, image in ipairs(gui:GetDescendants()) do
		if image:IsA("ImageLabel") then
			if button.BackgroundColor3 == image.ImageColor3 then
				image.Transparency = 0
			end
		end
	end
end)

For the behavior that you described (in the latter part of your response) you’ll need to differentiate between and store which images have had their colors changed as opposed to the images which already had their colors set.

local gui = script:FindFirstAncestorWhichIsA("ScreenGui")
local button = script.Parent
local updatedImages = {}

local function createImageColorSignals()
	for _, image in ipairs(gui:GetChildren()) do
		if image:IsA("ImageLabel") then
			image:GetPropertyChangedSignal("ImageColor3"):Connect(function()
				table.insert(updatedImages, image)
			end)
		end
	end
end
createImageColorSignals()

button.MouseButton1Click:Connect(function()
	for _, image in ipairs(updatedImages) do
		if button.BackgroundColor3 == image.ImageColor3 then
			image.Transparency = 0
		end
	end
end)

I already differentiated the ImageLabels, which the ones marked as “Space” being the ones that change colors and what I want to flash.

Oh, in that case.

for _, image in ipairs(gui:GetDescendants()) do
	if image.Name == "Space" then
		--do specific action for images named space
	end
end

Should be as simple as searching for GuiObjects named “Space”.

I have different ImageLabels named “Space” where they are followed by a number (i.e. “Space1”). That will work for this, right?

for _, image in ipairs(gui:GetDescendants()) do
	if image.Name:match("^Space") then
		--do specific action for images where name starts with "Space"
	end
end
1 Like

Alright. I was also planning on doing a looping of the flash in the script, but I’m not sure how to accomplish that. I can’t do a separate script for each ImageLabel, because that would be too complicated and the ImageButton script might not pick that up.

Flashing as in quickly toggling the transparency between 0 and 1?

That’s right. Of course, it wouldn’t be toggling, more so scripting it (i.e. One second, the transparency’s 0, then 1, etc.)

Yes, by toggling I’m referring to switching between the two states in quick succession.

for i = 1, 10 do
	task.wait(0.1)
	image.Transparency = i % 2
end
1 Like

Would I have to change the “10” from “1, 10” to reflect how many ImageLabels I want to toggle?

That loop would be for each ImageLabel instance, the loop iterates for 10 cycles, toggling the transparency between 0 and 1 with a 0.1 second interval between each state change. Because you have multiple ImageLabels which need changing simultaneously you’ll need to create additional threads (so that the main thread isn’t yielded).

task.spawn(function()
	for i = 1, 10 do
		task.wait(0.1)
		image.Transparency = i % 2
	end
end)

Also the numbers used were just an example, you may wish to have the flashing occur more frequently, for a longer duration, more interval between each flash etc.

1 Like

Alright. For the “i % 2” section, what does that mean?

% is the modulo operator in Lua, it returns the remainder when dividing the first value by the second, so in this case when i is 1, 1 % 2 returns 1 as 1 is the remainder (1 cannot go into 2), on the second time round i is 2 and 2 divided by 2 has a remainder of 0.

What about merging the script? So far, I got the “^Space” script added, but I’m trying to figure out how to add the task one in.

Can you send what you have so far?

1 Like

Here’s what I have thus far:

local gui = script:FindFirstAncestorWhichIsA("ScreenGui")
local button = script.Parent
local updatedImages = {}

local function createImageColorSignals()
	for _, image in ipairs(gui:GetDescendants()) do
		if image.Name:match("^Space") then
			--do specific action for images where name starts with "Space"
		end
	end
end
createImageColorSignals()

button.MouseButton1Click:Connect(function()
	for _, image in ipairs(updatedImages) do
		if button.BackgroundColor3 == image.ImageColor3 then
			image.Transparency = 0
		end
	end
end)