Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

0

Btw, the MouseClick event passes the player who clicked the detector in the first argument.

0

Ah thanks

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

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.