Multiple ClickDetectors

Hello

So I want to fire few ClickDetectors at the same time instead of firing them on a script one by one. Is there a way to fire many ClickDetectors at once or should I just fire them manually?

1 Like

You mean have several functions for several different click detectors in one script? Like

clickdetector1.MouseButton1Click:Connect(function()
end)
clickdetector2.MouseButton1Click:Connect(function()
end)

Is there a way to fire both of them at the same time?

I’m confused what you mean by that. Fire click detectors at same time? Like clicking them?

1 Like

Is there a way to fire both of them at the same time?

To my understanding, a user can’t click two clickdetectors at the exact same time, so I don’t think this is possible.

I have a drawer with a few parts, and I put a ClickDetector in each of them.

So you want one function to handle several different click detectors?

2 Likes

A user can’t click multiple ClickDetectors at the exact same time.

As Btkelley17 previously said, you would most likely to use multiple functions in one script.

I know they can’t click multiple, I just want to know if there is a way to make a function for each ClickDetector in one function instead of writting

script.Parent.ClickDetector.MouseButton1Click:Connect(function()
end)
1 Like

Assuming the drawer is a model, and this script is directly under the model, try this:

for i,v in pairs(script.Parent:GetDescendants()) do
if v.ClassName == “ClickDetector” then
v.MouseClick:Connect(function()
– Handle clickdetector code
end)
end
end

2 Likes

Checking all the Detectors and when they are clicked.

something like

local function Clicked()
-- do stuff here
end

for i,v in pairs(PATHHERE:GetChildren()) do
if v:IsA("ClickDetector") then
v.MouseClick:Connect(Clicked)
end 
end

Basically what trixroux said

1 Like

If the drawer is a Model then you can just parent a single ClickDetector to the Model instance itself and use that. All the BasePart descendants of the Model will be clickable.

3 Likes

Really, I never knew I can do that. Thanks.

How do I check Which one of this parts got clicked?

There’s really no way to check that through ClickDetectors. Only one thing you could do is using Mouse.Button1Up event. Something like this maybe?

local Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.Button1Up:Connect(function()
    if Mouse.Target == [TargetLocation] then
        -- Your code
    end
end)

Again this isn’t the cleanest way to do it.

Edit 1: WHY WOULD YOU NECROBUMP THE POST

2 Likes