Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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

Log in to vote
1
Answered by 2 years ago

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

0
Alright thanks! I'll test it later! hellmet1 42 — 2y
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? hellmet1 42 — 2y
0
This is what i tried doing, look at the answers hellmet1 42 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
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
0
Consider changing __object.Mouseclick to __object.MouseClick (case sensitivity threw an error for me), other than that, the implementation looks fine loowa_yawn 383 — 2y
1
Thank you so so much dude! I've been trying to do fix this for many days! hellmet1 42 — 2y

Answer this question