Is there a way to detect if the player clicks a random clickdetector?

I know u can do this whit proximity prompts but i did not found anything on clickdetectors

1 Like

What do you mean by a random clickdetector? Do you mean you choose a random clickdetector to be clicked, or something else?

1 Like

Checking which player clicked a click detector is simple as it’s the first parameter of the .MouseClick event, see below.

local ClickDetector = workspace.PathToClickDetector -- wherever your click detector is

ClickDetector.MouseClick:Connect(function(plr)
	print(plr.Name.." clicked the click detector named "..ClickDetector.Name) -- output as an example would be: playerName clicked the click detector named PathToClickDetector
end)

If you wanted to do this for every click detector, you could use CollectionService to do so, and loop through each tagged instance and connect a MouseClick event to it. Keep in mind this does require you to tag each ClickDetector, which is made simple with plugins such as “Tag Editor.” Here is an example of that.

local CollectionService = game:GetService("CollectionService")
local TaggedClickDetectors = CollectionService:GetTagged("TagName")

for _, cd in pairs(TaggedClickDetectors) do
	if cd:IsA("ClickDetector") then -- might be useless if you know everything tagged is a ClickDetector already but here is a check to confirm.
		cd.MouseClick:Connect(function(plr)
			print(plr.Name.." clicked the click detector named "..cd.Name)
		end)
	end
end
1 Like