Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i select the clickdetector of many buttons in a folder?

So, I am making an elevator that has a fading gui when you click a button, just to clarify, this script isn't mine, but how do i get the clickdetector of every part in the folder?

local Frame = script.Parent
local Buttons = game.Workspace.Buttons:GetChildren()
local ClickDetector = -- i wanna get the clickdetector of many parts with the same name in a folder here
local TweenS = game:GetService("TweenService")
local Db = false
local FadeTime = 3
ClickDetector.MouseClick:Connect(function()
    if not Db then
        Db = true
        TweenS:Create(Frame,TweenInfo.new(0.5),{BackgroundTransparency = 0}):Play()
        task.wait(FadeTime)
        TweenS:Create(Frame,TweenInfo.new(0.5),{BackgroundTransparency = 1}):Play()
        task.wait(FadeTime)
        Db = false
    end
end)

2 answers

✓ Accepted answer

If you are looking to get every click detector inside a given folder (either as a child of a child, child of a child of a child, etc..):

you can use GetDescendants() and IsA(), both respectfully getting every object inside an object, and checking if a Roblox object is a specific type

local ClickDetectors = {}
local Folder = workspace:WaitForChild('ClickDetectors') 
-- change this to the location of your folder

for __index, __object in ipairs(Folder:GetDescendants()) do
	if __object:IsA('ClickDetector') then
		table.insert(ClickDetectors, __object)
		-- or do something with '__object'
		-- as it is confirmed to be a clickdetector
	end
end

fyi i have not personally tested this code myself

3 comments

0

Alright thanks! I'll test it later!

0

Dont really get how i should put this in my script, sorry I'm a beginner. could you copy it and put it in it?

0

This is what i tried doing, look at the answers

local Frame = script.Parent
local ClickDetectors = {}
local Folder = workspace:WaitForChild('Buttons')
local TweenS = game:GetService("TweenService")
local Db = false
local FadeTime = 3
for __index, __object in ipairs(Folder:GetDescendants()) do
    if __object:IsA('ClickDetector') then
        table.insert(ClickDetectors, __object)
		__object.Mouseclick:Connect(function()
			if not Db then
				Db = true
				TweenS:Create(Frame,TweenInfo.new(0.5),{BackgroundTransparency = 0}):Play()
				task.wait(FadeTime)
				TweenS:Create(Frame,TweenInfo.new(0.5),{BackgroundTransparency = 1}):Play()
				task.wait(FadeTime)
				Db = false
			end
		end)
    end
end

2 comments

0

Consider changing __object.Mouseclick to __object.MouseClick (case sensitivity threw an error for me), other than that, the implementation looks fine

1

Thank you so so much dude! I've been trying to do fix this for many days!

This discussion is preserved as it was. New answers, comments, and votes are closed.