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

Are you able to listen for all clickdetector events?

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

✓ Accepted answer

Yes. You can listen to all ClickDetectors events. Here is an example that I made for you:

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.

1 comment

0

Thank you! I thought of this method but i didn't think the last for loop would would thank your pointing this out!

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