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

How to make ClickDetector fire when it's a child in a model?

Asked by
oggy521 72
5 years ago

I currently have a model which consists of multiple different parts. I wish for the model to trigger an event when it's clicked through the use of click detectors:

ClickDetector.MouseClick:connect()

Because the model is made up of several parts, I would rather the Mouseclick to trigger no matter which part of the block is clicked and therefore I've added a click detector to every child of the model using a for loop.

However, this is where the problem comes in, how do I phrase it so that the event would trigger when any child of the model's click detector is clicked without having to copy and paste a few hundred lines to accommodate for each part in the model

2 answers

Log in to vote
1
Answered by 5 years ago

You can get all the ClickDetector's using a loop and store it in a table using table.insert. From there, you have access to all detectors and can detect when one is clicked in a single script:

local Model = script.Parent

local Detectors = {}

function getDetectors()
    for i, v in pairs(Model:GetDescendants()) do 
        if v:IsA("ClickDetector") then 
            table.insert(Detectors, v)
        end
    end
end
getDetectors()

--Now, use a loop to get all the detectors stored in the table:
for i = 1, #Detectors do 
    local detector = Detectors[i]
    detector.MouseClick:Connect(function(player)
        -- event code
    end)
end
0
Btw, the MouseClick event passes the player who clicked the detector in the first argument. awesomeipod 607 — 5y
0
Ah thanks oggy521 72 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

i think the only answer for this is creating a invisible part, wich fits in the model's box then just insert a ClickDetector in the Invisible Part

0
Hm, but I do distinctly remember there was a way to trigger multiple events which are in the children of the model you're using. oggy521 72 — 5y

Answer this question