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

Are you able to listen for all clickdetector events?

Asked by 5 years ago

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

1 answer

Log in to vote
0
Answered by
JakyeRU 637 Moderation Voter
5 years ago
Edited by User#24403 5 years ago

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.

0
Thank you! I thought of this method but i didn't think the last for loop would would thank your pointing this out! XxHyperglassxX 4 — 5y
Ad

Answer this question