I know this explanation is terrible, but I don’t know how else to explain it.
So, basically, every mannequin in my game has a click-detector. How can I automatically find all of them? (With this I mean, if I add or delete a mannequin, how can I trigger the function with the click-detector in this mannequin too?)
Use ChildAdded and add all of the mannequins to a common location e.g a folder.
local folder = workspace.Manneqquins
folder.ChildAdded:Connect(function(newChild) --new child is a manequin
local clicker = newChild.ClickDetector
clicker.MouseClick:Connect(--[[enter function here --]])
end)
Also, loop through the folder contents when the game is first run, in case you have mannequins there to start off with.
ChildAdded will run for every new mannequin that is added to the workspace (specifically in the folder that you specify). You’re making a connection to every mannequin, so there’s no limit to the number of times the function will run. The function will run for every time one of the mannequins are clicked.
The only exception (as I mentioned in the previous post) is for pre-existing models. You can account for them by simply running a loop as so.
for _, v in pairs(folder:GetChildren()) do
local clicker = v.ClickDetector
clicker.MouseClick:Connect(--[[same function name here--]])
end
That particular line of code will only run once, but the connection had already been made, so the corresponding function will run (as I mentioned before) for every time the mannequin is clicked. If there’s some other functionality that you require, you have to specify more clearly what you’re looking for.
To clarify, clicker.MouseClick is the event which will fire every time a player clicks on a click detector. When this event fires, the corresponding function (which you write in where I commented) will be run.
The reason why this works for every new mannequin that is added, is because of this line being wrapped inside a function connecting to ChildAdded. Every time a child is added, the new mannequin’s click detector is given a similar connection.
Again, there’s no limit here to how many times the function runs.