Hey I'm trying to listen for all the clickdetector MouseClick event without doing all the functions manually because i have 100 parts to check. I'm trying to do this for my a* algorithm but I'm stuck.
local tab = {} local closed = {} local open = {} for i,v in pairs(workspace:GetChildren()) do if v.Name == 'Part' then tab[#tab + 1] = v Instance.new("ClickDetector", v) end end math.randomseed(tick()) local r = math.random(1,#tab) local startingPoint = tab[r] startingPoint.BrickColor = BrickColor.new('Really red') local cl = startingPoint:FindFirstChild('ClickDetector') cl:Destroy()
Yes. You can listen to all ClickDetectors events. Here is an example that I made for you:
```lua local ClickDetectors = {}
for _, v in pairs(game.Workspace:GetChildren()) do -- This will get everything that is in the Workspace.
if v:FindFirstChildWhichIsA("ClickDetector") then -- This will check if it has a ClickDetector in it.
table.insert(ClickDetectors,v) -- This will insert the part in the table we just created. (local ClickDetectors = {})
end
end
print(#ClickDetectors) -- This will print the number of elements inside the table. This is optional. --// Don't use getn !!!
for _, v in pairs(ClickDetectors) do -- This will get everything from the table.
v.ClickDetector.MouseClick:Connect(function() -- This will listen for the event MouseClick from every ClickDetector
print("Clicked!")
end)
end
-- Now, if you click any Part it will print once "Clicked!". ``` I hope I helped you.