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
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
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