Multiple click detectors

what i want is after these click detectors are generated i want them to be linked to that clicked() function but when i click them nothing prints out i know i can use collection service but i want to know if i can do it like this ?

function Clicked()
	print('clicked')
end


local function GetDetectors()
	for i , v in pairs(GreenParts) do
		local clickdetector = Instance.new("ClickDetector")
		count += 1
		clickdetectors[count] = clickdetector
		clickdetector.Name = ('clickdetector'.. count )
		clickdetector.Parent = v
		clickdetector.MouseClick:Connect(Clicked)
	end

count = 0

end
1 Like

the issue is that you use TABLE on mouse click signal

	clickdetectors.MouseClick:Connect(Clicked)

should use a click detector instead. Try this:

	clickdetector.MouseClick:Connect(Clicked)

that ‘s’ was accident the script i have doesn’t contain that “s”

in fact im useing this
clickdetectors[count].MouseClick:Connect(Clicked)

show me the full correct code.

function Clicked()
print(‘clicked’)
end

local function GetDetectors()
for i , v in pairs(GreenParts) do
local clickdetector = Instance.new(“ClickDetector”)
count += 1
clickdetectors[count] = clickdetector
clickdetector.Name = (‘clickdetector’… count )
clickdetector.Parent = v
clickdetectors[count].MouseClick:Connect(Clicked)
end

count = 0

end

Did you call the function? Because it shouldn’t work if you didn’t do so

euh… do u see the word after connect it is the function above

Not the connect part, from the code snippet you didn’t call GetDetectors() somewhere

You have to call it so it will run on the main thread

please, use preformatted text

function Clicked()
print(‘clicked’)
end

local function GetDetectors()
for i , v in pairs(GreenParts) do
local clickdetector = Instance.new(“ClickDetector”)
count += 1
clickdetectors[count] = clickdetector
clickdetector.Name = (‘clickdetector’… count )
clickdetector.Parent = v
clickdetectors[count].MouseClick:Connect(Clicked)
end

count = 0

end
  1. You don’t need variable count, in fact you have index in a for loop.

  2. why you’d put click detector into array and do thought the it signal connection?

  3. have you make sure click detector exist? Have you tried debugging? Is console clear?

where do i put this since this is the first time i see and besides does it matter if clickdetectors is insiide a part which is inside a model??

If it’s a script/localscript then just GetDetectors()

they are alreday inside a table clickdetecotrs is a table

oh im stupid i thought clickdetectors is a built-in function xD and yeah they are being called and i verified it by printing the clickdetectors table

I made a working code snippet here, match it to yours workflow… The code is working
Basically I tried to understand what you want to achieve and this is the right way to do so

local greenParts: Folder = workspace.greenParts
local clickDetectorsArr: {} = {}

local function onClick()
	print("I was clicked")
end

local function initializeDetectors()
	for index, part in pairs(greenParts:GetChildren()) do
		local clickDetector = Instance.new("ClickDetector")
		clickDetector.Name = string.format("Click_detector %i", index)
		clickDetector.Parent = part
		clickDetector.MouseClick:Connect(onClick)
		clickDetectorsArr[clickDetector.Name] = clickDetector;
	end
end

initializeDetectors()

first greenparts is just a table and im copying that table to use the number of instances inside it to run the loop as many as instances inside that table so u can say it is useless right now
and i tried to create 6 different part inside a model and do the same thing but generateing click detectors before the game starts and it works

xDD this is the same response i got from bing ai i find it funny sorry im trying to be rude i just find it funny anyways either count = count +1 or count += 1 are the same u can try it and besides if this doesn’t work i would get an error instanatly when i run the script

well i found the issue
the problem was i had a ray cast that fires whenever i click on my mouse so if i click on that click detector it will get deleted before the clickdetector instance work

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.