Detect if one of multiple imagebuttons are pressed

I’m trying to detect if one of any of the imagebuttons is pressed in my GUI, the only problem is I have zero clue of how to do that. But basically, I’m trying to make it so that one is pressed the rest fade out while the one pressed stays visible. And then a delay before the one clicked fades out. I do sort of have an idea on how to do that, but I know my way would be messy (write a line for each button that fades it out) plus I would have to do that for each button combination. I don’t really have any idea how to go about writing this though but here’s what I have so far:

local z = script.Parent.z
local x = script.Parent.x
local c = script.Parent.c
local v = script.Parent.v
local TweenService = game:GetService("TweenService")
local time = 3
local FadeOut = TweenService:Create(script.Parent.z, TweenInfo.new(time), {ImageTransparency = 0})

FadeOut:Play()
FadeOut.Completed:Wait()

I actually didn’t know this was a thing at first, but it is.

You can actually check for click events in loops.

So I would do something like:

local buttons = {
	script.Parent.z;
	script.Parent.x;
	script.Parent.c;
	script.Parent.v;
}

local TweenService = game:GetService("TweenService")

local pressed = false
local pressedButton
local t = 3 --Instead of time, since "time" is in the luau dictionary

for i,v in pairs(buttons)do
	v.MouseButton1Click:Connect(function()
		if pressed == false then
			pressed = true
			pressedButton = v

			for i,v in pairs(buttons)do
				if v ~= pressedButton then
					local FadeOut = TweenService:Create(v, TweenInfo.new(t), {ImageTransparency = 0})

					FadeOut:Play()
				end
			end

			task.wait(t)
		end
	end)
end

hmm I do like this solution better than mine, but this isn’t working on my end, no errors though either.

Try printing throughout it, like right inside the for loop do print(v.Name) and make sure it prints all the button names.


local buttons = {
	script.Parent.z;
	script.Parent.x;
	script.Parent.c;
	script.Parent.v;
}


local TweenService = game:GetService("TweenService")
local time = 3

local pressed = false
local pressedButton

for i,v in pairs(buttons) do
	v.MouseButton1Click:Connect(function()
		pressedButton = v
		pressed = true
	for i,v in pairs(script.Parent:GetDescendants()) do
			if v ~= pressedButton and v.Name ~= "LocalScript" then
			local FadeOut = TweenService:Create(v, TweenInfo.new(time), {ImageTransparency = 1})

	FadeOut:Play()


	end
	end
end)
end

this work for me

[/quote]

Tried this, still have no idea why it isn’t working all of my print’s worked fine

--you just need to add 
	if pressed == false then
-- and
task.wait(t)

Try printing where the tween is created.

I suppose I should have clarified this better in my description, basically, whatever button is pressed I want the rest of them to tween transparency, side note I’m getting an error of some sort on line 22

are they textbuttons? if they are then change ImageTransparency to BackGroundTransparency

No, they are all image buttons

can we see the directory? because if there is anything other than the imagesbutton then you need to add it in the script on the first if (he other three should disappear all togeter right?)

I’ll just give you the .rbx file which contains the GUI and script
_quicktime.rbxm (5.6 KB)

local buttons = {
	script.Parent.z;
	script.Parent.x;
	script.Parent.c;
	script.Parent.v;
}


local TweenService = game:GetService("TweenService")
local t = 3

local pressed = false
local pressedButton

for i,v in pairs(buttons) do
	v.MouseButton1Click:Connect(function()
if not pressed then		
		pressedButton = v
		pressed = true
	for i,v in pairs(script.Parent:GetDescendants()) do
				if v ~= pressedButton and v.Name ~= "LocalScript" and v.Name ~= "Script2" and v.Name ~= "_ctext" and v.Name ~= "_vtext" and v.Name ~= "_xtext" and v.Name ~= "_ztext" then
			local FadeOut = TweenService:Create(v, TweenInfo.new(t), {ImageTransparency = 1})

	FadeOut:Play()

end
			end
		task.wait(t)	
	end
end)
end

if you want more things to not disappear then change this line

f v ~= pressedButton and v.Name ~= "LocalScript" and v.Name ~= "Script2" and v.Name ~= "_ctext" and v.Name ~= "_vtext" and v.Name ~= "_xtext" and v.Name ~= "_ztext" then

every thing in this line do not disappear

This is working for me, but how would I do the opposite for the text, as in making everything but the corresponding text disappear (i.e: _ztext and z when z is pressed)?

like if you press z then the z button and text disappear?

No as in you press the z button and every text but z text disappears.

No, because that does not work

What are you having trouble with? Also why the heck did this man steal my code and make small changes to it lol.

Well, my problem is, although the other buttons their corresponding text isn’t. As for your second question :man_shrugging: